﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;
using LPV_POS.In;
//using TextPDF;
using CrystalDecisions.Shared;
using LPV_POS.Baocao;
using LPV_POS.mh_baocao;
using CrystalDecisions.CrystalReports.Engine;
using LPV_POS.mh_pos.mh_doanhthu;
using LPV_POS.mh_pos;
using LPV.SendMail;
using System.Resources;
using System.Globalization;

namespace LPV_POS
{
    public partial class baocaodoanhthu : DevExpress.XtraEditors.XtraForm
    {
        CultureInfo culture;
        xulyngaythangnam ngaythang;
        TimeSpan sp;
        thuvien tv;
        DataTable tb;
        DataSet ds;
        string[] s;
        object[] o;
        private string str_loaibaocao = "";

        decimal sumTienmat = 0;           //---bc theo httt, --editor: PL, date: 2012.07.24
        decimal sumThe = 0;               //---bc theo httt, --editor: PL, date: 2012.07.24
        decimal SumTienmatThuchi = 0;     //---bc theo httt, --editor: PL, date: 2012.07.24
        DataTable dtienmat;             //---bc theo httt, --editor: PL, date: 2012.07.24
        DataTable dThe;                 //---bc theo httt, --editor: PL, date: 2012.07.24
        DataTable dt_bcthe;              //---bc theo httt, --editor: PL, date: 2012.07.24


        #region "Muti language"

        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang6_PhuThu).Assembly);
                bton_tungaydenngay.Text = rm.GetString("tungaydenngay", culture);
                label9.Text = rm.GetString("khohang", culture);
                label1.Text = rm.GetString("ngayhethong", culture);
                lbel_kho.Text = rm.GetString("baocaokhohang", culture);
                lbel_khuvuc_ban.Text = rm.GetString("baocaoban", culture);
                lbel_khachhang.Text = rm.GetString("baocaokhachhang", culture);
                lble_nhanvien.Text = rm.GetString("baocaonhanvien", culture);
                lbel_NguyenLieu.Text = rm.GetString("baocaonguyenlieu", culture);
                lbel_inbaocao.Text = rm.GetString("inbaocao", culture);
                lbel_fileexcel.Text = rm.GetString("xuatexcel", culture);
                lblBCThe.Text = rm.GetString("baocaonhomchuthe", culture);
                lbel_Ket_Thuc_Ngay.Text = rm.GetString("ketthucngaylamviec", culture);
                lbel_nhomchu.Text = rm.GetString("baocaonhom", culture);
                lbel_nhomhang.Text = rm.GetString("baocaomon", culture);
                lbel_CongNo_KhachHang.Text = rm.GetString("congnokhachhang", culture);
                check_bc_Z.Text = rm.GetString("thoat", culture);
                lblNguoiGioiThieu.Text = rm.GetString("nguoigioithieu", culture);
                LblBCNVPV.Text = rm.GetString("baocaonhanvienphucvu", culture);
                lblHistory_Bill_Item.Text = rm.GetString("lichsuxoabillmon", culture);
                lblThe_Tra_Truoc.Text = rm.GetString("thetratruoc", culture);
                lbel_Bao_cao_LaiLo.Text = rm.GetString("baocaolailo", culture);
                bton_xoatatcaBC.Text = rm.GetString("xoatatcabaocao", culture);

                label10.Text = rm.GetString("phidichvu", culture);
                lbel_giamgiamathang1.Text = rm.GetString("phuthu", culture);
                label8.Text = rm.GetString("khuyenmai", culture);
                label12.Text = rm.GetString("vat", culture);
                label7.Text = rm.GetString("tientip", culture);
                lbel_vatmathang1.Text = rm.GetString("thu", culture);
                lbel_vathoadon1.Text = rm.GetString("chi", culture);
                label6.Text = rm.GetString("khachhangno", culture);
                lbel_DatCoc_Text.Text = rm.GetString("datcoc", culture);
                label11.Text = rm.GetString("trudatcoc", culture);
                label16.Text = rm.GetString("tongcuoi", culture);
                lbel_tientrongket1.Text = rm.GetString("tientrongket", culture);
                BtnGuiMail.Text = rm.GetString("goimail", culture);
                lbel_tongcongchuatanggiam1.Text = rm.GetString("tong_1", culture);
                lbel_tongcongdatanggiam1.Text = rm.GetString("tong_2", culture);
                label16.Text = rm.GetString("tong_3", culture);
                lbel_giamgia1.Text = rm.GetString("giamgiabillthucan", culture);

                lbl_The.Text = rm.GetString("the", culture);        //---bc theo httt, --editor: PL, date: 2012.07.24
                lbl_Tienmat.Text = rm.GetString("tienmat", culture);//---bc theo httt, --editor: PL, date: 2012.07.24

                // nhom chu
                this.grid_doanhthu_nhomchu.GroupPanelText = rm.GetString("nhomchu", culture);
                this.col_nhomchu.Caption= rm.GetString("nhomchu", culture);
                this.col_thanhtien.Caption= rm.GetString("thanhtien", culture);
                this.col_tienmat.Caption = rm.GetString("tienmat", culture);
                // nhom hang
                this.grid_doanhthu_nhomhang.GroupPanelText = rm.GetString("nhomhang", culture);
                this.col_nhomhang.Caption = rm.GetString("nhomhang", culture);
                this.col_thanhtien_nhomhang.Caption = rm.GetString("thanhtien", culture);
                this.col_tienmat_nhomhang.Caption = rm.GetString("tienmat", culture);
                // hang hoa
                this.grid_doanhthu_hanghoa.GroupPanelText = rm.GetString("mon", culture);
                this.col_hanghoa.Caption = rm.GetString("nhomhang", culture);
                this.col_thanhtien_hanghoa.Caption = rm.GetString("thanhtien", culture);
                this.col_tienmat_hanghoa.Caption = rm.GetString("tienmat", culture);
                this.col_soluong_hanghoa.Caption = rm.GetString("soluong", culture);
                // doanh thu kho hang
                this.grid_doanhthu_khohang.GroupPanelText = rm.GetString("khohang", culture);
                this.col_sanpham_khohang.Caption = rm.GetString("mon", culture);
                this.col_ton_kho_ban_dau_kho.Caption = rm.GetString("tondau", culture);
                this.col_nhapkho_kho.Caption = rm.GetString("nhap", culture);
                this.col_xuatkho_kho.Caption = rm.GetString("xuat", culture);
                this.col_nhaptralai_kho.Caption = rm.GetString("tralai", culture);
                this.col_banra_kho.Caption = rm.GetString("banra", culture);
                this.col_toncuoiky_kho.Caption = rm.GetString("toncuoi", culture);
                this.col_luong_hang_hoa_thay_doi.Caption = rm.GetString("tienmat", culture);
                // kho hang chi tiết nhập xuất
                this.grid_khonhapxuat_kho.GroupPanelText = rm.GetString("khohang", culture);
                this.col_sanpham_kho.Caption = rm.GetString("mon", culture);
                this.col_thaydoi_kho.Caption = rm.GetString("thaydoi", culture);
                this.col_ngaygio_kho.Caption = rm.GetString("ngaygio", culture);
                this.col_loai_kho.Caption = rm.GetString("loai", culture);
               // nhan vien tong quat
                this.grid_doanhthu_nhanvien_tongquat.GroupPanelText = rm.GetString("nhanvientongquat", culture);
                this.col_luotvaogannhat_nvtq.Caption = rm.GetString("luotvaogannhat", culture);
                this.col_luotragannhat_nvtq.Caption = rm.GetString("luotragannhat", culture);
                this.col_luotbanhangdautien_nvtq.Caption = rm.GetString("luotbanhangdautien", culture);
                this.col_luot_ban_hang_cuoi_cung_nvtq.Caption = rm.GetString("luotbanhangcuoicung", culture);
                this.col_dc_nvtq.Caption = rm.GetString("giamgia", culture);
                // nhan vien chi tiet vao ra
                this.grid_doanhthu_nhanvien_chitietvaora.GroupPanelText = rm.GetString("nhanvienchitietvaora", culture);
                this.col_ten_nhanvienvara.Caption = rm.GetString("ten", culture);
                this.col_vao_nhanvienvaora.Caption = rm.GetString("vaoca", culture);
                this.col_ra_nhanvienvaora.Caption = rm.GetString("raca", culture);
                this.col_trongkhoang_nhanvienvaora.Caption = rm.GetString("trongkhoang", culture);
                // nhan vien chi tiet
                this.grid_nhanvien_chitiet.GroupPanelText = rm.GetString("nhanvienchitiet", culture);
                this.col_ngayhethong_nv.Caption = rm.GetString("ngayhethong", culture);
                this.col_so_nv.Caption = rm.GetString("nhanvienID", culture);
                this.col_tongcong_nv.Caption = rm.GetString("tongcong", culture);
                this.col_tienmat_nv.Caption = rm.GetString("tienmat", culture);
                this.col_DC_nv.Caption = rm.GetString("giamgia", culture);
                this.col_the_nv.Caption = rm.GetString("the", culture);
                // nhan vien chi tiet mat hang
                this.grid_nhanvien_chitietmathang.GroupPanelText = rm.GetString("nhanvienchitietmathang", culture);
                this.gridColumn1.Caption = rm.GetString("mon", culture);
                this.gridColumn2.Caption = rm.GetString("soluong", culture);
                this.gridColumn3.Caption = rm.GetString("thanhtien", culture);
                this.gridColumn4.Caption = rm.GetString("giamgia", culture);
                this.gridColumn5.Caption = rm.GetString("tongcong", culture);
                this.col_the_nv.Caption = rm.GetString("the", culture);
                // khach hang tong quat
                this.grid_khachhangtongquat_1.GroupPanelText = rm.GetString("khachhangtongquat", culture);
                this.gridColumn16.Caption = rm.GetString("the", culture);
                this.gridColumn17.Caption = rm.GetString("soluong", culture);
                this.gridColumn18.Caption = rm.GetString("tongtien", culture);
                this.gridColumn19.Caption = rm.GetString("luotvaogannhat", culture);
                // khach hang mat hang
                this.grid_khachhang_mathang.GroupPanelText = rm.GetString("khachhangmathang", culture);
                this.col_sanpham_khachhang.Caption = rm.GetString("mon", culture);
                this.col_soluong_khachhang.Caption = rm.GetString("khachhang", culture);
                this.col_cong_khachhang.Caption = rm.GetString("thanhtien", culture);
                this.col_DC_khachhang.Caption = rm.GetString("giamgia", culture);
                this.col_sales_khachhang.Caption = rm.GetString("tongtien", culture);
                // khach hang chi tiet so
                this.grid_khachhangchitietso.GroupPanelText = rm.GetString("khachhangchitietso", culture);
                this.gridColumn10.Caption = rm.GetString("ngayhethong", culture);
                this.gridColumn11.Caption = rm.GetString("thanhtien", culture);
                this.gridColumn13.Caption = rm.GetString("giamgia", culture);
                this.gridColumn12.Caption = rm.GetString("tongtien", culture);
                //the thanh toan chi tiet
                this.col_loaithe.Caption = rm.GetString("loaithe", culture);    //---bc theo httt, --editor: PL, date: 2012.07.24
                this.col_tienmat.Caption = rm.GetString("tongtien", culture);   //---bc theo httt, --editor: PL, date: 2012.07.24


                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion
        // select danh sach kho hang
        private DataTable f_selectkhohangforcom()
        {
            tv = new thuvien();
            s = new string[] { "@flag" };
            o = new object[] { 0 };
            tb = tv.SP_SelectDataTable("sp_baocaodoanhthu", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["OBJNAME"] = "--- Tất cả kho ---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }
        //-------------------------------------------------- Begin New ---------------------------------------------------------------------------------------------------------
        private DataSet f_selectdoanhthu_nhom(Int16 loaidoanhthu, string date_tungay, string date_denngay, string makho, Boolean loaibaocao)
        {
            tv = new thuvien();
            s = new string[] { "@FLAG", "@FROMDATE", "@TODATE", "@MAKHOHANG", "@LOAIBAOCAO" };
            o = new object[] { loaidoanhthu, date_tungay, date_denngay, makho, loaibaocao };
            ds = tv.SP_SelectDataset("sp_baocaodoanhthu_hanghoa", s, o);
            if (!new cpublic().KiemTra_CoQuyen(this.Name, "lbel_nhomchu", 1))//2011.10.18
            {

                foreach (DataTable tb in ds.Tables)
                    tb.Rows.Clear();
            }
            return ds;
        }

        private DataSet f_selectdoanhthu_gio(Boolean baocaotrongngay, DateTime ngaygio, DateTime date_tungay, DateTime date_denngay, string makho, Boolean bao_cao_Z)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@BAO_CAO_TRONG_NGAY", "@NGAYGIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@MAKHOHANG", "@BAO_CAO_Z" };
            o = new object[] { 1, baocaotrongngay, ngaygio, date_tungay, date_denngay, makho, bao_cao_Z };
            ds = tv.SP_SelectDataset("sp_baocaodoanhthu_gio", s, o);

            return ds;
        }


        private DataSet f_selectdoanhthu_khuvuc_ban(Int16 loaidoanhthu, string date_tungay, string date_denngay, string makho, Boolean loaibaocao)
        {
            tv = new thuvien();
            s = new string[] { "@FLAG", "@FROMDATE", "@TODATE", "@MAKHOHANG", "@LOAIBAOCAO" };
            o = new object[] { loaidoanhthu, date_tungay, date_denngay, makho, loaibaocao };
            ds = tv.SP_SelectDataset("sp_baocaodoanhthu_ban", s, o);
            if (!new cpublic().KiemTra_CoQuyen(this.Name, "lbel_khuvuc_ban", 1))//2011.10.18
            {

                foreach (DataTable tb in ds.Tables)
                    tb.Rows.Clear();
            }
            return ds;
        }

        private DataSet f_selectdoanhthu_nhanvien_tongquat(Boolean baocaotrongngay, DateTime ngaygio, DateTime date_tungay, DateTime date_denngay, string makho, Boolean bao_cao_Z)
        {
            tv = new thuvien();
            s = new string[] { "@BAO_CAO_TRONG_NGAY", "@NGAYGIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@MAKHOHANG", "@BAO_CAO_Z" };
            o = new object[] { baocaotrongngay, ngaygio, date_tungay, date_denngay, makho, bao_cao_Z };
            ds = tv.SP_SelectDataset("sp_baocaodoanhthu_nhanvien1_tongquat", s, o);
            if (!new cpublic().KiemTra_CoQuyen(this.Name, "lble_nhanvien", 1))//2011.10.18
            {

                foreach (DataTable tb in ds.Tables)
                    tb.Rows.Clear();
            }
            return ds;
        }
        private DataSet f_selectdoanhthu_nhanvien_chitietvaora(Boolean baocaotrongngay, DateTime ngaygio, DateTime date_tungay, DateTime date_denngay, string makho, Boolean bao_cao_Z)
        {
            tv = new thuvien();
            s = new string[] { "@BAO_CAO_TRONG_NGAY", "@NGAYGIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@MAKHOHANG", "@BAO_CAO_Z" };
            o = new object[] { baocaotrongngay, ngaygio, date_tungay, date_denngay, makho, bao_cao_Z };
            ds = tv.SP_SelectDataset("sp_baocaodoanhthu_nhanvien2_chitiet_vaora", s, o);
            if (!new cpublic().KiemTra_CoQuyen(this.Name, "lble_nhanvien", 1))//2011.10.18
            {

                foreach (DataTable tb in ds.Tables)
                    tb.Rows.Clear();
            }
            return ds;
        }

        private DataSet f_selectdoanhthu_nhanvien_chitiet(Boolean baocaotrongngay, DateTime ngaygio, DateTime date_tungay, DateTime date_denngay, string makho, Boolean bao_cao_Z)
        {
            tv = new thuvien();
            s = new string[] { "@BAO_CAO_TRONG_NGAY", "@NGAYGIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@MAKHOHANG", "@BAO_CAO_Z" };
            o = new object[] { baocaotrongngay, ngaygio, date_tungay, date_denngay, makho, bao_cao_Z };
            ds = tv.SP_SelectDataset("sp_baocaodoanhthu_nhanvien3_chitiet", s, o);
            if (!new cpublic().KiemTra_CoQuyen(this.Name, "lble_nhanvien", 1))//2011.10.18
            {

                foreach (DataTable tb in ds.Tables)
                    tb.Rows.Clear();
            }
            return ds;
        }

        private DataSet f_selectdoanhthu_nhanvien_chitietmathang(Boolean baocaotrongngay, DateTime ngaygio, DateTime date_tungay, DateTime date_denngay, string makho, Boolean bao_cao_Z, string manhanvien)
        {
            tv = new thuvien();
            s = new string[] { "@BAO_CAO_TRONG_NGAY", "@NGAYGIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@MAKHOHANG", "@MANHANVIEN", "@BAO_CAO_Z" };
            o = new object[] { baocaotrongngay, ngaygio, date_tungay, date_denngay, makho, manhanvien, bao_cao_Z };
            ds = tv.SP_SelectDataset("sp_baocaodoanhthu_nhanvien4_chitiet_mathang", s, o);
            if (!new cpublic().KiemTra_CoQuyen(this.Name, "lble_nhanvien", 1))//2011.10.18
            {

                foreach (DataTable tb in ds.Tables)
                    tb.Rows.Clear();
            }
            return ds;
        }

        private DataSet f_selectdoanhthu_nhanvien_phieu(string date_tungay, string date_denngay, string makho, string manhanvien, Boolean loaibaocao)
        {
            tv = new thuvien();
            s = new string[] { "@FLAG", "@FROMDATE", "@TODATE", "@MAKHOHANG", "@MANHANVIEN", "@LOAIBAOCAO" };
            o = new object[] { 1, date_tungay, date_denngay, makho, manhanvien, loaibaocao };
            ds = tv.SP_SelectDataset("sp_baocaodoanhthu_nhanvien5_phieu", s, o);
            if (!new cpublic().KiemTra_CoQuyen(this.Name, "lble_nhanvien", 1))//2011.10.18
            {

                foreach (DataTable tb in ds.Tables)
                    tb.Rows.Clear();
            }
            return ds;
        }

        private DataSet f_selectdoanhthu_khohang(Boolean baocaotrongngay, DateTime ngaygio, DateTime date_tungay, DateTime date_denngay, string makho, Boolean bao_cao_Z)
        {
            tv = new thuvien();
            s = new string[] { "@BAO_CAO_TRONG_NGAY", "@NGAYGIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@MAKHOHANG", "@BAO_CAO_Z" };
            o = new object[] { baocaotrongngay, ngaygio, date_tungay, date_denngay, makho, bao_cao_Z };
            ds = tv.SP_SelectDataset("sp_baocaokho_kho", s, o);

            return ds;
        }

        private DataSet f_selectdoanhthu_khohang_nhapxuat(Boolean baocaotrongngay, DateTime ngaygio, DateTime date_tungay, DateTime date_denngay, string makho, Boolean bao_cao_Z)
        {
            tv = new thuvien();
            s = new string[] { "@BAO_CAO_TRONG_NGAY", "@NGAYGIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@MAKHOHANG", "@BAO_CAO_Z" };
            o = new object[] { baocaotrongngay, ngaygio, date_tungay, date_denngay, makho, bao_cao_Z };
            ds = tv.SP_SelectDataset("sp_baocaokho_chitiet_nhapxuat", s, o);
            return ds;
        }

        private DataSet f_selectdoanhthu_khachhang_mathang(Boolean baocaotrongngay, DateTime ngaygio, DateTime date_tungay, DateTime date_denngay, string makho, Boolean bao_cao_Z)
        {
            tv = new thuvien();
            s = new string[] { "@BAO_CAO_TRONG_NGAY", "@NGAYGIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@MAKHOHANG", "@BAO_CAO_Z" };
            o = new object[] { baocaotrongngay, ngaygio, date_tungay, date_denngay, makho, bao_cao_Z };
            ds = tv.SP_SelectDataset("sp_baocaokhachhang3_chitiet_mathang", s, o);
            if (!new cpublic().KiemTra_CoQuyen(this.Name, "lbel_khachhang", 1))//2011.10.18
            {
                foreach (DataTable tb in ds.Tables)
                    tb.Rows.Clear();
            }
            return ds;
        }

        private DataSet f_selectdoanhthu_khachhang_tongquat(Boolean baocaotrongngay, DateTime ngaygio, DateTime date_tungay, DateTime date_denngay, string makho, Boolean bao_cao_Z)
        {
            tv = new thuvien();
            s = new string[] { "@BAO_CAO_TRONG_NGAY", "@NGAYGIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@MAKHOHANG", "@BAO_CAO_Z" };
            o = new object[] { baocaotrongngay, ngaygio, date_tungay, date_denngay, makho, bao_cao_Z };
            ds = tv.SP_SelectDataset("sp_baocaokhachhang1_tongquat", s, o);
            if (!new cpublic().KiemTra_CoQuyen(this.Name, "lbel_khachhang", 1))//2011.10.18
            {

                foreach (DataTable tb in ds.Tables)
                    tb.Rows.Clear();
            }
            return ds;
        }

        private DataSet f_selectdoanhthu_khachhang_chitietso(Boolean baocaotrongngay, DateTime ngaygio, DateTime date_tungay, DateTime date_denngay, string makho, Boolean bao_cao_Z)
        {
            tv = new thuvien();
            s = new string[] { "@BAO_CAO_TRONG_NGAY", "@NGAYGIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@MAKHOHANG", "@BAO_CAO_Z" };
            o = new object[] { baocaotrongngay, ngaygio, date_tungay, date_denngay, makho, bao_cao_Z };
            ds = tv.SP_SelectDataset("sp_baocaokhachhang2_chitiet_so", s, o);
            if (!new cpublic().KiemTra_CoQuyen(this.Name, "lbel_khachhang", 1))//2011.10.18
            {

                foreach (DataTable tb in ds.Tables)
                    tb.Rows.Clear();
            }
            return ds;
        }

        //                                       
        //-----------------In doanh thu tất cả-------------26/06/2012------------
        private DataSet f_selectdoanhthu_tatca(string fromdate, string todate, string makho, Boolean loaibaocao)
        {

            tv = new thuvien();
            s = new string[] { "@FROMDATE_all", "@TODATE_all", "@MAKHOHANG_all", "@LOAIBAOCAO_all" };
            o = new object[] { fromdate, todate, makho, loaibaocao };
            //s = new string[] { "@BAO_CAO_TRONG_NGAY", "@NGAYGIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@MAKHOHANG", "@BAO_CAO_Z" };
            //o = new object[] { baocaotrongngay, ngaygio, date_tungay, date_denngay, makho ,bao_cao_Z};
            ds = tv.SP_SelectDataset("sp_baocaodoanhthu_tatca", s, o);
            return ds;
        }

        //-------------------------------------------------- End New ---------------------------------------------------------------------------------------------------------


        //// select doanh thu theo nhom hang
        //private DataSet f_selectdoanhthu_nhomhang(Boolean loaibaocao,Boolean gio, DateTime date_tungay, DateTime date_denngay, string tungay, string denngay, string makho)
        //{
        //    tv = new thuvien();
        //    s = new string[] { "@flag","@LOAIBAOCAO", "@GIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@TUNGAY_STR", "@DENNGAY_STR", "@MAKHOHANG" };
        //    o = new object[] { 2, loaibaocao, gio, date_tungay, date_denngay, tungay, denngay, makho };
        //    ds = tv.SP_SelectDataset("sp_baocaodoanhthu", s, o);
        //    return ds;  
        //}
        //// select doanh thu theo nhom chu
        //private DataSet f_selectdoanhthu_nhomchu(Boolean loaibaocao, Boolean gio, DateTime date_tungay, DateTime date_denngay, string tungay, string denngay, string makho)
        //{
        //    tv = new thuvien();
        //    s = new string[] { "@flag", "@LOAIBAOCAO", "@GIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@TUNGAY_STR", "@DENNGAY_STR", "@MAKHOHANG" };
        //    o = new object[] { 1, loaibaocao, gio, date_tungay, date_denngay, tungay, denngay, makho };
        //    ds = tv.SP_SelectDataset("sp_baocaodoanhthu", s, o);
        //    return ds;
        //}

        //// select doanh thu theo hang hoa
        //private DataSet f_selectdoanhthu_hanghoa(Boolean loaibaocao,Boolean gio, DateTime date_tungay, DateTime date_denngay, string tungay, string denngay, string makho)
        //{
        //    tv = new thuvien();
        //    s = new string[] { "@flag", "@LOAIBAOCAO", "@GIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@TUNGAY_STR", "@DENNGAY_STR", "@MAKHOHANG" };
        //    o = new object[] { 7, loaibaocao, gio, date_tungay, date_denngay, tungay, denngay, makho };
        //    ds = tv.SP_SelectDataset("sp_baocaodoanhthu", s, o);
        //    return ds;
        //}


        // select doanh thu theo nhan vien
        private DataSet f_selectdoanhthu_nhanvien(Boolean loaibaocao, Boolean gio, DateTime date_tungay, DateTime date_denngay, string tungay, string denngay, string makho)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@LOAIBAOCAO", "@GIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@TUNGAY_STR", "@DENNGAY_STR", "@MAKHOHANG" };
            o = new object[] { 3, loaibaocao, gio, date_tungay, date_denngay, tungay, denngay, makho };
            ds = tv.SP_SelectDataset("sp_baocaodoanhthu", s, o);
            if (!new cpublic().KiemTra_CoQuyen(this.Name, "lble_nhanvien", 1))//2011.10.18
            {

                foreach (DataTable tb in ds.Tables)
                    tb.Rows.Clear();
            }
            return ds;
        }

        // select doanh thu theo khach hang
        private DataSet f_selectdoanhthu_khachhang(Boolean loaibaocao, Boolean gio, DateTime date_tungay, DateTime date_denngay, string tungay, string denngay, string makho)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@LOAIBAOCAO", "@GIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@TUNGAY_STR", "@DENNGAY_STR", "@MAKHOHANG" };
            o = new object[] { 4, loaibaocao, gio, date_tungay, date_denngay, tungay, denngay, makho };
            ds = tv.SP_SelectDataset("sp_baocaodoanhthu", s, o);
            if (!new cpublic().KiemTra_CoQuyen(this.Name, "lbel_khachhang", 1))//2011.10.18
            {

                foreach (DataTable tb in ds.Tables)
                    tb.Rows.Clear();
            }
            return ds;
        }

        // select doanh thu theo kho hang
        private DataSet f_selectdoanhthu_khohang(Boolean loaibaocao, Boolean gio, DateTime date_tungay, DateTime date_denngay, string tungay, string denngay)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@LOAIBAOCAO", "@GIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@TUNGAY_STR", "@DENNGAY_STR" };
            o = new object[] { 5, loaibaocao, gio, date_tungay, date_denngay, tungay, denngay };
            ds = tv.SP_SelectDataset("sp_baocaodoanhthu", s, o);
            return ds;
        }

        // select doanh thu theo khu vuc
        private DataSet f_selectdoanhthu_khuvuc(Boolean loaibaocao, Boolean gio, DateTime date_tungay, DateTime date_denngay, string tungay, string denngay, string makho)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@LOAIBAOCAO", "@GIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@TUNGAY_STR", "@DENNGAY_STR", "@MAKHOHANG" };
            o = new object[] { 6, loaibaocao, gio, date_tungay, date_denngay, tungay, denngay, makho };
            ds = tv.SP_SelectDataset("sp_baocaodoanhthu", s, o);
            if (!new cpublic().KiemTra_CoQuyen(this.Name, "lbel_khuvuc_ban", 1))//2011.10.18
            {

                foreach (DataTable tb in ds.Tables)
                    tb.Rows.Clear();
            }
            return ds;
        }

        // select doanh thu theo ban
        private DataSet f_selectdoanhthu_ban(Boolean loaibaocao, Boolean gio, DateTime date_tungay, DateTime date_denngay, string tungay, string denngay, string makho)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@LOAIBAOCAO", "@GIO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@TUNGAY_STR", "@DENNGAY_STR", "@MAKHOHANG" };
            o = new object[] { 8, loaibaocao, gio, date_tungay, date_denngay, tungay, denngay, makho };
            ds = tv.SP_SelectDataset("sp_baocaodoanhthu", s, o);
            if (!new cpublic().KiemTra_CoQuyen(this.Name, "lbel_khuvuc_ban", 1))//2011.10.18
            {

                foreach (DataTable tb in ds.Tables)
                    tb.Rows.Clear();
            }
            return ds;
        }

        // select tat ca cac doanh thu
        private DataSet f_selecttatcacacdoanhthu(Boolean loaibaocao, Boolean gio, DateTime date_tungay, DateTime date_denngay, string tungay, string denngay, string makho)
        {
            tv = new thuvien();
            s = new string[] { "@GIO", "@LOAIBAOCAO", "@DATE_TUNGAY", "@DATE_DENNGAY", "@TUNGAY_STR", "@DENNGAY_STR", "@MAKHOHANG" };
            o = new object[] { loaibaocao, gio, date_tungay, date_denngay, tungay, denngay, makho };
            ds = tv.SP_SelectDataset("sp_baocaodoanhthutatca1", s, o);
            return ds;
        }

        // select thong tin cong ty
        private DataTable f_selectgetinfo_company()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@CHOOSE" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mhcompany", s, o);
            return tb;
        }

        // ngay thang hien hanh
        private string f_chuoingayhienhanh()
        {
            string ngaythangnam = "";
            ngaythang = new xulyngaythangnam();
            tv = new thuvien();
            s = new string[] { "@flag", "@CHOOSE" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mhcompany", s, o);
            if (tb.Rows.Count > 0)
                ngaythangnam = ngaythang.chuoingay(tb.Rows[0]["NGAYHIENHANH"] + "");
            return ngaythangnam;
        }

        private int f_deletephieubanhang(string makho, Boolean loaibaocao)
        {
            int ketqua = -1;

            tv = new thuvien();
            s = new string[] { "@flag", "@MAKHOHANG", "@LOAIBAOCAO" };
            o = new object[] { 1, makho, loaibaocao };
            ketqua = tv.SP_Update("sp_xoabaocaodoanhthu", s, o);
            return ketqua;
        }

        private int f_deletephieubanhang_theongay(string tungay, string denngay, string makho)
        {
            int ketqua = -1;
            tv = new thuvien();
            s = new string[] { "@flag", "@TUNGAY_STR", "@DENNGAY_STR", "@MAKHOHANG" };
            o = new object[] { 2, tungay, denngay, makho };
            ketqua = tv.SP_Update("sp_xoabaocaodoanhthu", s, o);
            return ketqua;
        }

        private void lickdown(object sender, MouseEventArgs e)
        {
            //((Label)sender).BackColor = Color.FromArgb(192, 249, 251);
            //((Label)sender).BorderStyle = BorderStyle.Fixed3D;
        }

        private void lickup(object sender, MouseEventArgs e)
        {
            //((Label)sender).BackColor = Color.FromArgb(144, 196, 255);
            //((Label)sender).BorderStyle = BorderStyle.FixedSingle;
        }

        #region "          --- bao cao doanh thu theo hinh thuc thanh toan,  --editor: PL, date: 2012.07.25"
        private DataTable f_Tongtienmat(string date_tungay, string date_denngay, string makho, Boolean loaibaocao)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@FROMDATE", "@TODATE", "@MAKHOHANG", "@LOAIBAOCAO" };
            o = new object[] { 4, 2, date_tungay, date_denngay, makho, loaibaocao };
            dtienmat = tv.SP_SelectDataTable("sp_mh_dmTheTT", s, o);
            return dtienmat;
        }

        private DataTable f_TongThetratruoc(string date_tungay, string date_denngay, string makho, Boolean loaibaocao)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@FROMDATE", "@TODATE", "@MAKHOHANG", "@LOAIBAOCAO" };
            o = new object[] { 4, 1, date_tungay, date_denngay, makho, loaibaocao };
            dThe = tv.SP_SelectDataTable("sp_mh_dmTheTT", s, o);
            return dThe;
        }

        private DataTable f_bcThe(string date_tungay, string date_denngay, string makho, Boolean loaibaocao)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@FROMDATE", "@TODATE", "@MAKHOHANG", "@LOAIBAOCAO" };
            o = new object[] { 4, 3, date_tungay, date_denngay, makho, loaibaocao };
            dThe = tv.SP_SelectDataTable("sp_mh_dmTheTT", s, o);
            return dThe;
        }
        #endregion

        public baocaodoanhthu()
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
            tv = new thuvien();
            tv.f_showdataoncombox(com_khohang, f_selectkhohangforcom(), "OBJID", "OBJNAME");
            PhanQuyenGuiMail();
        }

        #region Phan quyen gui mail

        private void PhanQuyenGuiMail()
        {
            //Load Email
            DataSet ds = new thuvien().sql_SelectDataset("DM_COMPANY");
            if (ds != null)
            {
                BtnGuiMail.Visible = new cpublic().IsValidEmailAddress(ds.Tables[0].Rows[0]["EMAIL"].ToString() + "");
            }
        }

        #endregion

        private void f_chitietdoanhthu(DataSet _ds)
        {
            decimal iTHANHTIENTRUOC = 0;
            decimal iTHANHTIEN = 0;
            decimal iCHIECKHAU = 0;
            decimal iVat = 0;
            decimal iSERVICE_CHARGE = 0;
            decimal iPHUTHUKHAC = 0;
            decimal iKHUYENMAI = 0;
            decimal iTIENTIP = 0;
            decimal iTONGTHU = 0;
            decimal iTONGCHI = 0;
            decimal iCONGNO = 0;
            decimal iDatCoc = 0;
            decimal iDatCoc_Tru = 0;

            decimal.TryParse(_ds.Tables[0].Rows[0]["THANHTIENTRUOC"] + "", out iTHANHTIENTRUOC);
            decimal.TryParse(_ds.Tables[0].Rows[0]["THANHTIEN"] + "", out iTHANHTIEN);
            decimal.TryParse(_ds.Tables[0].Rows[0]["CHIECKHAU"] + "", out iCHIECKHAU);

            decimal.TryParse(_ds.Tables[0].Rows[0]["Vat"] + "", out iVat);
            decimal.TryParse(_ds.Tables[0].Rows[0]["SERVICE_CHARGE"] + "", out iSERVICE_CHARGE);
            decimal.TryParse(_ds.Tables[0].Rows[0]["PHUTHUKHAC"] + "", out iPHUTHUKHAC);
            decimal.TryParse(_ds.Tables[0].Rows[0]["KHUYENMAI"] + "", out iKHUYENMAI);
            decimal.TryParse(_ds.Tables[0].Rows[0]["TIENTIP"] + "", out iTIENTIP);
            decimal.TryParse(_ds.Tables[0].Rows[0]["TONGTHU"] + "", out iTONGTHU);
            decimal.TryParse(_ds.Tables[0].Rows[0]["TONGCHI"] + "", out iTONGCHI);
            decimal.TryParse(_ds.Tables[0].Rows[0]["CONGNO"] + "", out iCONGNO);
            if (_ds.Tables[0].Columns.Contains("DatCoc"))
                decimal.TryParse(_ds.Tables[0].Rows[0]["DatCoc"] + "", out iDatCoc);
            else
                iDatCoc = 0;

            if (_ds.Tables[0].Columns.Contains("DatCoc_Tru"))
                decimal.TryParse(_ds.Tables[0].Rows[0]["DatCoc_Tru"] + "", out iDatCoc_Tru);
            else
                iDatCoc_Tru = 0;


            lbel_tongcongchuatanggiam.Text = iTHANHTIENTRUOC.ToString("N0");
            lbel_giamgia_1.Text = iCHIECKHAU.ToString("N0");
            lbel_tongcongdagiamgia.Text = (decimal.Parse(lbel_tongcongchuatanggiam.Text.Replace(",", "")) - decimal.Parse(lbel_giamgia_1.Text.Replace(",", ""))).ToString("N0");
            lbel_giamgia_2.Text = lbel_giamgia_1.Text;
            lbel_phuthu_2.Text = iPHUTHUKHAC.ToString("N0");
            lbel_vat_2.Text = iVat.ToString("N0");
            lbel_tienchi_2.Text = iTONGCHI.ToString("N0");
            lbel_tienthu_2.Text = iTONGTHU.ToString("N0");
            lbel_thuedichvu_2.Text = iSERVICE_CHARGE.ToString("N0");
            lbel_tientip_2.Text = iTIENTIP.ToString("N0");
            lbel_congno.Text = iCONGNO.ToString("N0");
            lbel_datcoc.Text = iDatCoc.ToString("N0");
            lbel_datcoc_tru.Text = iDatCoc_Tru.ToString("N0");

            lbel_congno.Text = iCONGNO.ToString("N0");

            lbel_khuyenmai.Text = iKHUYENMAI.ToString("N0");

            //---------bc theo hinh thuc thanh toan -- editor: PL, date: 2012.07.25
            SumTienmatThuchi = sumTienmat + iTONGTHU - iTONGCHI + iDatCoc;

            lbl_The.Text = sumThe.ToString("N0");
            lbl_Tienmat.Text = SumTienmatThuchi.ToString("N0");
            //---------

            decimal iCONGNO_ALL = 0;
            if (_ds.Tables[0].Columns.Contains("CONGNO_ALL"))
                iCONGNO_ALL = decimal.Parse(_ds.Tables[0].Rows[0]["CONGNO_ALL"] + "");
            else
                iCONGNO_ALL = iCONGNO;
            string sab = (iTHANHTIENTRUOC
                                                - iCHIECKHAU
                                                + iVat
                                                + iSERVICE_CHARGE
                                                + iPHUTHUKHAC
                                                - iKHUYENMAI
                                                + iTIENTIP
                                                + iTONGTHU
                                                - iTONGCHI
                                                - iCONGNO
                                                + iDatCoc
                                                - iDatCoc_Tru
                                                ).ToString("N0");

            //lbel_datanggiam.Text = (decimal.Parse(lbel_tongcongchuatanggiam.Text.Replace(",", "")) - decimal.Parse(lbel_giamgia_1.Text.Replace(",", ""))                                                        //iTHANHTIEN
            //                        + iTONGTHU
            //                        - iTONGCHI//                                    + iPHUTHUKHAC
            //                        - iCONGNO_ALL
            //                        - iKHUYENMAI).ToString("N0");
            
            lbel_datanggiam.Text = sab;

            lbel_tientrongket.Text = lbel_datanggiam.Text;

        }

        private void f_xoachitietdoanhthu()
        {
            lbel_tongcongchuatanggiam.Text = "0";
            lbel_giamgia_1.Text = "0";
            lbel_giamgia_2.Text = "0";
            lbel_tienthu_2.Text = "0";
            lbel_vat_1.Text = "0";
            lbel_vat_2.Text = "0";
            lbel_tienchi_2.Text = "0";
            lbel_thuedichvu_1.Text = "0";
            lbel_thuedichvu_2.Text = "0";
            lbel_phuthu_1.Text = "0";
            lbel_tientip_1.Text = "0";
            lbel_tientip_2.Text = "0";
            lbel_phuthu_2.Text = "0";
            lbel_tongcongdagiamgia.Text = "0";
            lbel_datanggiam.Text = "0";
            lbel_tientrongket.Text = "0";
            lbel_congno.Text = "0";
            lbel_khuyenmai.Text = "0";
            lbel_datcoc.Text = "0";
            lbel_datcoc_tru.Text = "0";
            lbl_Tienmat.Text = "0";         //---------bc theo hinh thuc thanh toan -- editor: PL, date: 2012.07.25
            lbl_The.Text = "0";             //---------bc theo hinh thuc thanh toan -- editor: PL, date: 2012.07.25
        }

        private void mh_baocaodoanhthu_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
            string str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);

             
            mh_dmkhachhang omh_dmkhachhang = new mh_dmkhachhang();
            string skhachhang_op = ocpublic.Registry_Get(omh_dmkhachhang.Name + omh_dmkhachhang.txtkhachhang_optionbc.Name);
            if (skhachhang_op != "")
            {
                lbel_khachhang.Text = lbel_khachhang.Tag + " " + skhachhang_op;
                //
                grid_doanhthu_khachhang.GroupPanelText = skhachhang_op;
                grid_khachhang_mathang.GroupPanelText = skhachhang_op + " " + grid_khachhang_mathang.Tag;
                grid_khachhangchitietso.GroupPanelText = skhachhang_op + " chi tiết số";
                grid_khachhangtongquat_1.GroupPanelText = "Báo cáo " + skhachhang_op.ToLower() + " tổng quát";
            }

            #region "   ocpublic.BoGoc_Control"
            if (Variable.tbBH_CAU_HINH.Rows[0]["CAPNHAT_LAILO"] + "" != "True")
                lbel_Bao_cao_LaiLo.Text = "";

            if (Variable.tbBH_CAU_HINH.Rows[0]["NGAY_LAM_VIEC_LA_NGAY_HE_THONG"] + "" == "True")
                lbel_Ket_Thuc_Ngay.Text = "";

            if (ocpublic.Registry_Get("chkThanh_Toan_Bang_The_Tra_Truoc") != "True")
                lblThe_Tra_Truoc.Text = "";

            if (!ocpublic.KiemTra_CoQuyen(this.Name, lblHistory_Bill_Item.Name, 1))
                lblHistory_Bill_Item.Text = "";


            if (ocpublic.Registry_Get("lblHistory_Bill_Item") != "True")
                lblHistory_Bill_Item.Text = "";

            if (ocpublic.Registry_Get("LblBCNVPV") != "True")
                LblBCNVPV.Text = "";

            if (ocpublic.Registry_Get("lblNguoiGioiThieu") != "True")
                lblNguoiGioiThieu.Text = "";

            if (ocpublic.Registry_Get("lbel_inbaocao") != "True")
                lbel_inbaocao.Text = "";

            if (ocpublic.Registry_Get("lbel_fileexcel") != "True")
                lbel_fileexcel.Text = "";

            if (ocpublic.Registry_Get("lbel_nhomchu") != "True")
                lbel_nhomchu.Text = "";

            if (ocpublic.Registry_Get("lbel_nhomhang") != "True")
                lbel_nhomhang.Text = "";

            if (ocpublic.Registry_Get("lbel_NguyenLieu") != "True")
                lbel_NguyenLieu.Text = "";

            if (ocpublic.Registry_Get("lblBCThe") != "True")
                lblBCThe.Text = "";

            if (ocpublic.Registry_Get("lble_nhanvien") != "True")
                lble_nhanvien.Text = "";

            if (ocpublic.Registry_Get("lbel_khachhang") != "True")
                lbel_khachhang.Text = "";

            if (ocpublic.Registry_Get("lbel_khuvuc_ban") != "True")
                lbel_khuvuc_ban.Text = "";

            if (ocpublic.Registry_Get("lbel_kho") != "True")
                lbel_kho.Text = "";

            if (ocpublic.Registry_Get("lbel_CongNo_KhachHang") != "True")
                lbel_CongNo_KhachHang.Text = "";

            if (ocpublic.Registry_Get("lbel_CongNo_NhaCungCap") != "True")
                lbel_CongNo_NhaCungCap.Text = "";

            if (ocpublic.Registry_Get("lbl_bcThe") != "True") // bc httt, editor: PL, date: 2012.07.25
                lbl_bcThe.Text = "";                          // bc httt, editor: PL, date: 2012.07.25

            ocpublic.BoGoc_Control(lbel_inbaocao, lbel_inbaocao.BackColor, lbel_inbaocao.ForeColor, lbel_inbaocao.ForeColor, lbel_inbaocao.Font, true);
            ocpublic.BoGoc_Control(lbel_fileexcel, lbel_fileexcel.BackColor, lbel_fileexcel.ForeColor, lbel_fileexcel.ForeColor, lbel_fileexcel.Font, true);
            ocpublic.BoGoc_Control(lbel_nhomchu, lbel_nhomchu.BackColor, lbel_nhomchu.ForeColor, lbel_nhomchu.ForeColor, lbel_nhomchu.Font, true);
            ocpublic.BoGoc_Control(lbel_nhomhang, lbel_nhomhang.BackColor, lbel_nhomhang.ForeColor, lbel_nhomhang.ForeColor, lbel_nhomhang.Font, true);
            //ocpublic.BoGoc_Control(lbel_doanhthu_theogio, lbel_doanhthu_theogio.BackColor, lbel_doanhthu_theogio.ForeColor, lbel_doanhthu_theogio.ForeColor, lbel_doanhthu_theogio.Font, true);
            ocpublic.BoGoc_Control(lble_nhanvien, lble_nhanvien.BackColor, lble_nhanvien.ForeColor, lble_nhanvien.ForeColor, lble_nhanvien.Font, true);
            ocpublic.BoGoc_Control(lbel_khachhang, lbel_khachhang.BackColor, lbel_khachhang.ForeColor, lbel_khachhang.ForeColor, lbel_khachhang.Font, true);
            ocpublic.BoGoc_Control(lbel_khuvuc_ban, lbel_khuvuc_ban.BackColor, lbel_khuvuc_ban.ForeColor, lbel_khuvuc_ban.ForeColor, lbel_khuvuc_ban.Font, true);
            ocpublic.BoGoc_Control(lbel_kho, lbel_kho.BackColor, lbel_kho.ForeColor, lbel_kho.ForeColor, lbel_kho.Font, true);
            //ocpublic.BoGoc_Control(lblBaocao_TuanThang, lblBaocao_TuanThang.BackColor, lblBaocao_TuanThang.ForeColor, lblBaocao_TuanThang.ForeColor, lblBaocao_TuanThang.Font, true);
            ocpublic.BoGoc_Control(check_bc_Z, check_bc_Z.BackColor, check_bc_Z.ForeColor, check_bc_Z.ForeColor, check_bc_Z.Font, true);
            ocpublic.BoGoc_Control(lbel_NguyenLieu, lbel_NguyenLieu.BackColor, lbel_NguyenLieu.ForeColor, lbel_NguyenLieu.ForeColor, lbel_NguyenLieu.Font, true);
            ocpublic.BoGoc_Control(lbel_CongNo_KhachHang, lbel_CongNo_KhachHang.BackColor, lbel_CongNo_KhachHang.ForeColor, lbel_CongNo_KhachHang.ForeColor, lbel_CongNo_KhachHang.Font, true);
            ocpublic.BoGoc_Control(lbel_CongNo_NhaCungCap, lbel_CongNo_NhaCungCap.BackColor, lbel_CongNo_NhaCungCap.ForeColor, lbel_CongNo_NhaCungCap.ForeColor, lbel_CongNo_NhaCungCap.Font, true);
            ocpublic.BoGoc_Control(lbel_Bao_cao_LaiLo, lbel_Bao_cao_LaiLo.BackColor, lbel_Bao_cao_LaiLo.ForeColor, lbel_Bao_cao_LaiLo.ForeColor, lbel_Bao_cao_LaiLo.Font, true);
            ocpublic.BoGoc_Control(lbel_Ket_Thuc_Ngay, lbel_Ket_Thuc_Ngay.BackColor, lbel_Ket_Thuc_Ngay.ForeColor, lbel_Ket_Thuc_Ngay.ForeColor, lbel_Ket_Thuc_Ngay.Font, true);
            ocpublic.BoGoc_Control(lblThe_Tra_Truoc, lblThe_Tra_Truoc.BackColor, lblThe_Tra_Truoc.ForeColor, lblThe_Tra_Truoc.ForeColor, lblThe_Tra_Truoc.Font, true);
            ocpublic.BoGoc_Control(lblHistory_Bill_Item, lblHistory_Bill_Item.BackColor, lblHistory_Bill_Item.ForeColor, lblHistory_Bill_Item.ForeColor, lblHistory_Bill_Item.Font, true);
            //ANHLQ
            ocpublic.BoGoc_Control(LblBCNVPV, LblBCNVPV.BackColor, LblBCNVPV.ForeColor, LblBCNVPV.ForeColor, LblBCNVPV.Font, true);
            ocpublic.BoGoc_Control(lblBCThe, lblBCThe.BackColor, lblBCThe.ForeColor, lblBCThe.ForeColor, lblBCThe.Font, true);
            ocpublic.BoGoc_Control(lblNguoiGioiThieu, lblNguoiGioiThieu.BackColor, lblNguoiGioiThieu.ForeColor, lblNguoiGioiThieu.ForeColor, lblNguoiGioiThieu.Font, true);

            // bc httt, editor: PL, date: 2012.07.25
            ocpublic.BoGoc_Control(lbl_bcThe, lbl_bcThe.BackColor, lbl_bcThe.ForeColor, lbl_bcThe.ForeColor, lbl_bcThe.Font, true);

            #endregion

            ngaythang = new xulyngaythangnam();
            check_ngay.Checked = true;
            luoi_doanhthu.MainView = grid_doanhthu_nhomchu;
            luoi_doanhthu.DataSource = null;
            date_ngay_baocao_z.Enabled = true;
            date_ngayhethong.Enabled = true;
            com_khohang._SetselectedID("1");

            if (!new cpublic().KiemTra_CoQuyen(this.Name, lbel_nhomchu.Name, 1))
            {
                MessageBox_LPV.Show("Bạn không có quyền");
                return;
            }
            ds = f_selectdoanhthu_nhom(1, ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), !radio_baocaoX.Checked);

            //--------bc theo hinh thuc thanh toan,-- editor: PL, date: 2012.07.25
            dtienmat = f_Tongtienmat(ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), !radio_baocaoX.Checked);
            decimal.TryParse(dtienmat.Rows[0]["TONGTIEN"] + "", out sumTienmat);

            dThe = f_TongThetratruoc(ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), !radio_baocaoX.Checked);
            decimal.TryParse(dThe.Rows[0]["TONGTIEN"] + "", out sumThe);
            ////////--------------- 


            dsbaocao = ds.Copy();
            if (ds.Tables[0].Rows.Count > 0)// && (ds.Tables[0].Rows[0]["THANHTIENTRUOC"] + "") != "")
            {
                luoi_doanhthu.DataSource = ds.Tables[1];
                f_chitietdoanhthu(ds);
            }
            else
                f_xoachitietdoanhthu();

            str_loaibaocao = "nhomchu";
            txt_kieudoanhthu.Text = " NHÓM CHỦ";
        }

        private void lbel_inbaocao_MouseDown(object sender, MouseEventArgs e)
        {
            lickdown(sender, e);
        }

        private void lbel_inbaocao_MouseUp(object sender, MouseEventArgs e)
        {
            lickup(sender, e);
        }

        private void lbel_kho_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;
            ngaythang = new xulyngaythangnam();
            Variable.str_tungay = ngaythang.chuoingay(ngaythang.chuyenngay(date_ngayhethong.Text));
            if (!new cpublic().KiemTra_CoQuyen(this.Name, ((System.Windows.Forms.Control)sender).Name, 1))
            {
                MessageBox_LPV.Show("Bạn không có quyền");
                return;
            }

            mh_list_khohang frm_khohang = new mh_list_khohang();
            frm_khohang.ShowDialog();
            if (frm_khohang.DialogResult == DialogResult.Cancel)
                return;

            DateTime NGAYGIO = new DateTime(date_ngayhethong.DateTime.Year, date_ngayhethong.DateTime.Month, date_ngayhethong.DateTime.Day, sp.Hours, sp.Minutes, 0);
            if (radio_baocaoZ.Checked)
                NGAYGIO = new DateTime(date_ngay_baocao_z.DateTime.Year, date_ngay_baocao_z.DateTime.Month, date_ngay_baocao_z.DateTime.Day, sp.Hours, sp.Minutes, 0);
            this.Cursor = Cursors.WaitCursor;
            switch (frm_khohang.str_loaibaocao)
            {
                case 1:    // bao cao kho hang
                    str_loaibaocao = "kho";
                    luoi_doanhthu.MainView = grid_doanhthu_khohang;
                    luoi_doanhthu.DataSource = null;

                    dsbaocao = f_selectdoanhthu_khohang(true, date_ngayhethong.DateTime, date_ngayhethong.DateTime, date_ngayhethong.DateTime, com_khohang._GetselectedID(), radio_baocaoZ.Checked);
                    luoi_doanhthu.DataSource = dsbaocao.Tables[0];
                    txt_kieudoanhthu.Text = " Kho hàng";
                    break;
                case 2:   // bao cao chi tiet nhap xuat ton
                    str_loaibaocao = "kho_nhapxuat";
                    luoi_doanhthu.MainView = grid_khonhapxuat_kho;
                    luoi_doanhthu.DataSource = null;

                    dsbaocao = f_selectdoanhthu_khohang_nhapxuat(true, NGAYGIO, date_ngayhethong.DateTime, date_ngayhethong.DateTime, com_khohang._GetselectedID(), radio_baocaoZ.Checked);
                    if (dsbaocao != null)
                    {
                        luoi_doanhthu.DataSource = dsbaocao.Tables[0];
                        txt_kieudoanhthu.Text = " Kho hàng";
                    }
                    break;
            }
            this.Cursor = Cursors.Default;
            return;

            //Boolean hienthigio = false;
            //Boolean loaibaocao = false;
            //ngaythang = new xulyngaythangnam();
            //luoi_doanhthu.MainView = grid_doanhthu_khohang;
            //luoi_doanhthu.DataSource = null;

            //if (radio_baocaoX.Checked == true)
            //    loaibaocao = false;
            //else
            //    loaibaocao = true;

            //if (check_hiengio.Checked == true)
            //    hienthigio = true;
            //else
            //    hienthigio = false;
            //    ds = f_selectdoanhthu_khohang(loaibaocao,hienthigio, date_ngay_baocao_z.DateTime, date_ngayhethong.DateTime, ngaythang.chuyenngay(date_ngay_baocao_z.Text), ngaythang.chuyenngay(date_ngayhethong.Text));
            //    dsbaocao = ds.Copy();
            //    if (ds.Tables[0].Rows.Count > 0 && (ds.Tables[0].Rows[0]["THANHTIENTRUOC"] + "") != "")
            //    {
            //        luoi_doanhthu.DataSource = ds.Tables[2];
            //        f_chitietdoanhthu(ds);
            //    }
            //    else
            //        f_xoachitietdoanhthu();

            txt_kieudoanhthu.Text = " Kho hàng";
        }

        private void check_ngay_CheckedChanged(object sender, EventArgs e)
        {
            if (check_ngay.Checked == true)
            {

                date_ngay_baocao_z.Properties.DisplayFormat.FormatString = "dd/MM/yyyy";
                date_ngay_baocao_z.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                date_ngay_baocao_z.Properties.EditFormat.FormatString = "dd/MM/yyyy";
                date_ngay_baocao_z.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                date_ngay_baocao_z.Properties.EditMask = "dd/MM/yyyy";

                date_ngayhethong.Properties.DisplayFormat.FormatString = "dd/MM/yyyy";
                date_ngayhethong.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                date_ngayhethong.Properties.EditFormat.FormatString = "dd/MM/yyyy";
                date_ngayhethong.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                date_ngayhethong.Properties.EditMask = "dd/MM/yyyy";

                date_ngay_baocao_z.DateTime = DateTime.Today;
                date_ngayhethong.DateTime = DateTime.Today;
                date_ngay_baocao_z.Enabled = true;
                date_ngayhethong.Enabled = true;
                check_hiengio.Enabled = true;
                check_hiengio.Checked = false;
            }
            else
            {
                date_ngay_baocao_z.Text = "";
                date_ngayhethong.Text = "";
                date_ngay_baocao_z.Enabled = false;
                date_ngayhethong.Enabled = false;
                check_hiengio.Enabled = false;
            }

        }

        private void lbel_nhomchu_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;
            if (!new cpublic().KiemTra_CoQuyen(this.Name, ((System.Windows.Forms.Control)sender).Name, 1))
            {
                MessageBox_LPV.Show("Bạn không có quyền");
                return;
            }
            ngaythang = new xulyngaythangnam();
            Variable.str_tungay = ngaythang.chuoingay(ngaythang.chuyenngay(date_ngayhethong.Text));
            mh_list_bc_nhom frm_list_bc_nhom = new mh_list_bc_nhom();
            frm_list_bc_nhom.ShowDialog();
            if (frm_list_bc_nhom.DialogResult == DialogResult.Cancel)
                return;

            //--------bc theo hinh thuc thanh toan,-- editor: PL, date: 2012.07.25
            dtienmat = f_Tongtienmat(ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), !radio_baocaoX.Checked);
            decimal.TryParse(dtienmat.Rows[0]["TONGTIEN"] + "", out sumTienmat);

            dThe = f_TongThetratruoc(ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), !radio_baocaoX.Checked);
            decimal.TryParse(dThe.Rows[0]["TONGTIEN"] + "", out sumThe);
            ////////--------------- 

            switch (frm_list_bc_nhom.str_loaibaocao)
            {
                case "nhomchu":
                    str_loaibaocao = "nhomchu";
                    luoi_doanhthu.MainView = grid_doanhthu_nhomchu;
                    luoi_doanhthu.DataSource = null;
                    if (radio_baocaoX.Checked == true)
                        dsbaocao = f_selectdoanhthu_nhom(1, ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), false);
                    else

                        dsbaocao = f_selectdoanhthu_nhom(1, ngaythang.chuyenngay(date_ngay_baocao_z.Text), ngaythang.chuyenngay(date_ngay_baocao_z.Text), com_khohang._GetselectedID(), true);

                    txt_kieudoanhthu.Text = " NHÓM CHỦ";

                    break;
                case "nhomhang":
                    str_loaibaocao = "nhomhang";
                    luoi_doanhthu.MainView = grid_doanhthu_nhomhang;
                    luoi_doanhthu.DataSource = null;
                    if (radio_baocaoX.Checked == true)
                        dsbaocao = f_selectdoanhthu_nhom(2, ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), false);
                    else
                        dsbaocao = f_selectdoanhthu_nhom(2, ngaythang.chuyenngay(date_ngay_baocao_z.Text), ngaythang.chuyenngay(date_ngay_baocao_z.Text), com_khohang._GetselectedID(), true);
                    txt_kieudoanhthu.Text = " NHÓM HÀNG";
                    break;
            }

            if (dsbaocao.Tables.Count < 0)
                return;
            if (dsbaocao.Tables[0].Rows.Count > 0)// && (dsbaocao.Tables[0].Rows[0]["THANHTIENTRUOC"] + "") != "")
            {
                luoi_doanhthu.DataSource = dsbaocao.Tables[1];
                f_chitietdoanhthu(ds);
            }
            else
                f_xoachitietdoanhthu();


        }

        private void lbel_nhomhang_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;
            if (!new cpublic().KiemTra_CoQuyen(this.Name, ((System.Windows.Forms.Control)sender).Name, 1))
            {
                MessageBox_LPV.Show("Bạn không có quyền");
                return;
            }
            ngaythang = new xulyngaythangnam();
            Variable.str_tungay = ngaythang.chuoingay(ngaythang.chuyenngay(date_ngayhethong.Text));
            luoi_doanhthu.MainView = grid_doanhthu_nhomhang;
            luoi_doanhthu.DataSource = null;

            str_loaibaocao = "mathang";
            luoi_doanhthu.MainView = grid_doanhthu_hanghoa;
            luoi_doanhthu.DataSource = null;
            if (radio_baocaoX.Checked)
                dsbaocao = f_selectdoanhthu_nhom(3, ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), false);
            else
                dsbaocao = f_selectdoanhthu_nhom(3, ngaythang.chuyenngay(date_ngay_baocao_z.Text), ngaythang.chuyenngay(date_ngay_baocao_z.Text), com_khohang._GetselectedID(), true);

            if (dsbaocao.Tables[0].Rows.Count > 0)// && (dsbaocao.Tables[0].Rows[0]["THANHTIENTRUOC"] + "") != "")
            {
                //--------bc theo hinh thuc thanh toan,-- editor: PL, date: 2012.07.25
                dtienmat = f_Tongtienmat(ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                decimal.TryParse(dtienmat.Rows[0]["TONGTIEN"] + "", out sumTienmat);

                dThe = f_TongThetratruoc(ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                decimal.TryParse(dThe.Rows[0]["TONGTIEN"] + "", out sumThe);
                ////////--------------- 

                luoi_doanhthu.DataSource = dsbaocao.Tables[1];
                f_chitietdoanhthu(ds);
            }
            else
                f_xoachitietdoanhthu();

            txt_kieudoanhthu.Text = "MẶT HÀNG";

        }

        private void lble_nhanvien_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;
            if (!new cpublic().KiemTra_CoQuyen(this.Name, ((System.Windows.Forms.Control)sender).Name, 1))
            {
                MessageBox_LPV.Show("Bạn không có quyền");
                return;
            }
            ngaythang = new xulyngaythangnam();
            Variable.str_tungay = ngaythang.chuoingay(ngaythang.chuyenngay(date_ngayhethong.Text));
            mh_list_nhanvien frm_list_nhanvien = new mh_list_nhanvien();
            frm_list_nhanvien.ShowDialog();
            if (frm_list_nhanvien.DialogResult != DialogResult.OK)
                return;

            DateTime dNgayBC = date_ngay_baocao_z.DateTime;

            if (radio_baocaoX.Checked)
                dNgayBC = date_ngayhethong.DateTime;

            switch (frm_list_nhanvien.str_loaibaocao)
            {
                case 1:
                    str_loaibaocao = "nv_tongquat";
                    luoi_doanhthu.MainView = grid_doanhthu_nhanvien_tongquat;
                    luoi_doanhthu.DataSource = null;
                    if (radio_baocaoX.Checked == true)
                        dsbaocao = f_selectdoanhthu_nhanvien_tongquat(true, dNgayBC, date_ngayhethong.DateTime, date_ngayhethong.DateTime, com_khohang._GetselectedID(), false);
                    else
                        dsbaocao = f_selectdoanhthu_nhanvien_tongquat(true, dNgayBC, date_ngay_baocao_z.DateTime, date_ngay_baocao_z.DateTime, com_khohang._GetselectedID(), true);
                    txt_kieudoanhthu.Text = " Nhân viên";
                    break;
                case 2:
                    str_loaibaocao = "nv_chitietvaora";
                    luoi_doanhthu.MainView = grid_doanhthu_nhanvien_chitietvaora;
                    luoi_doanhthu.DataSource = null;
                    if (radio_baocaoX.Checked == true)
                        dsbaocao = f_selectdoanhthu_nhanvien_chitietvaora(true, dNgayBC, date_ngayhethong.DateTime, date_ngayhethong.DateTime, com_khohang._GetselectedID(), false);
                    else
                        dsbaocao = f_selectdoanhthu_nhanvien_chitietvaora(true, dNgayBC, date_ngay_baocao_z.DateTime, date_ngay_baocao_z.DateTime, com_khohang._GetselectedID(), true);

                    txt_kieudoanhthu.Text = " Nhân viên chi tiết vào ra";
                    break;

                case 3:
                    str_loaibaocao = "nv_chitiet";
                    luoi_doanhthu.MainView = grid_nhanvien_chitiet;
                    luoi_doanhthu.DataSource = null;
                    if (radio_baocaoX.Checked == true)
                        dsbaocao = f_selectdoanhthu_nhanvien_chitiet(true, dNgayBC, date_ngayhethong.DateTime, date_ngayhethong.DateTime, com_khohang._GetselectedID(), false);
                    else
                        dsbaocao = f_selectdoanhthu_nhanvien_chitiet(true, dNgayBC, date_ngay_baocao_z.DateTime, date_ngay_baocao_z.DateTime, com_khohang._GetselectedID(), true);
                    txt_kieudoanhthu.Text = " Nhân viên chi tiết";
                    break;

                case 7:
                    str_loaibaocao = "nv_chitietmathang";
                    luoi_doanhthu.MainView = grid_nhanvien_chitietmathang;
                    luoi_doanhthu.DataSource = null;
                    string SmAnv = this.sGetNhanVien_fromList();
                    if (radio_baocaoX.Checked == true)
                        dsbaocao = f_selectdoanhthu_nhanvien_chitietmathang(true, dNgayBC, date_ngayhethong.DateTime, date_ngayhethong.DateTime, com_khohang._GetselectedID(), false, SmAnv);
                    else
                        dsbaocao = f_selectdoanhthu_nhanvien_chitietmathang(true, dNgayBC, date_ngay_baocao_z.DateTime, date_ngay_baocao_z.DateTime, com_khohang._GetselectedID(), true, SmAnv);

                    txt_kieudoanhthu.Text = " Nhân viên chi tiết mặt hàng";
                    break;

                case 8:
                    str_loaibaocao = "Chi tiet phieu";
                    luoi_doanhthu.MainView = grid_nhanvien_phieu;
                    luoi_doanhthu.DataSource = null;
                    //if (radio_baocaoX.Checked == true)
                    //    dsbaocao = f_selectdoanhthu_nhanvien_phieu(string.Format("{0:yyyyMMdd}", date_ngayhethong.DateTime), string.Format("{0:yyyyMMdd}", date_ngayhethong.DateTime), com_khohang._GetselectedID(), Variable.manhanvien, false);
                    //else
                    //    dsbaocao = f_selectdoanhthu_nhanvien_phieu(string.Format("{0:yyyyMMdd}", date_ngayhethong.DateTime), string.Format("{0:yyyyMMdd}", date_ngayhethong.DateTime), com_khohang._GetselectedID(), Variable.manhanvien, true);
                    SmAnv = this.sGetNhanVien_fromList();

                    dsbaocao = f_selectdoanhthu_nhanvien_phieu(string.Format("{0:yyyyMMdd}", date_ngayhethong.DateTime), string.Format("{0:yyyyMMdd}", date_ngayhethong.DateTime), com_khohang._GetselectedID(), SmAnv, !radio_baocaoX.Checked);
                    txt_kieudoanhthu.Text = "doanh thu phieu";
                    // Variable.manhanvien = "";
                    break;

            }

            luoi_doanhthu.DataSource = dsbaocao.Tables[0];
            f_xoachitietdoanhthu();
            return;


        }
        string sGetNhanVien_fromList()
        {
            string SmAnv = "";
            #region "   nhan vien"
            mh_dmnhanvien frm_dmnhanvien = new mh_dmnhanvien();
            frm_dmnhanvien.m_nhanvien_baocao = true;
            DataRow drNV = frm_dmnhanvien.ShowForm();            
            if (drNV != null)
                SmAnv = drNV["OBJID"] + "";
            #endregion
            return SmAnv;
        }
        private void lbel_khachhang_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;
            if (!new cpublic().KiemTra_CoQuyen(this.Name, ((System.Windows.Forms.Control)sender).Name, 1))
            {
                MessageBox_LPV.Show("Bạn không có quyền");
                return;
            }
            ngaythang = new xulyngaythangnam();
            Variable.str_tungay = ngaythang.chuoingay(ngaythang.chuyenngay(date_ngayhethong.Text));
            mh_list_khachhang frm_list_khachhang = new mh_list_khachhang();
            frm_list_khachhang.ShowDialog();
            if (frm_list_khachhang.DialogResult == DialogResult.Cancel)
                return;

            DateTime NGAYGIO;
            NGAYGIO = new DateTime(date_ngayhethong.DateTime.Year, date_ngayhethong.DateTime.Month, date_ngayhethong.DateTime.Day, sp.Hours, sp.Minutes, 0);
            if (radio_baocaoZ.Checked)
                NGAYGIO = new DateTime(date_ngay_baocao_z.DateTime.Year, date_ngay_baocao_z.DateTime.Month, date_ngay_baocao_z.DateTime.Day, sp.Hours, sp.Minutes, 0);
            switch (frm_list_khachhang.str_loaibaocao)
            {
                case 1:    // bao cao khach hang tong quat
                    str_loaibaocao = "khachhang_tongquat";
                    luoi_doanhthu.MainView = grid_khachhangtongquat_1;
                    luoi_doanhthu.DataSource = null;

                    if (radio_baocaoX.Checked == true)
                        dsbaocao = f_selectdoanhthu_khachhang_tongquat(true, NGAYGIO, date_ngayhethong.DateTime, date_ngayhethong.DateTime, com_khohang._GetselectedID(), false);
                    else
                        dsbaocao = f_selectdoanhthu_khachhang_tongquat(true, NGAYGIO, date_ngay_baocao_z.DateTime, date_ngay_baocao_z.DateTime, com_khohang._GetselectedID(), true);
                    if (dsbaocao.Tables.Count > 0)
                        luoi_doanhthu.DataSource = dsbaocao.Tables[0];
                    txt_kieudoanhthu.Text = " Khách hàng";
                    break;

                case 2:    // bao cao khach hang chi tiet so
                    str_loaibaocao = "khachhang _ chitietso";
                    luoi_doanhthu.MainView = grid_khachhangchitietso;
                    luoi_doanhthu.DataSource = null;
                    if (radio_baocaoX.Checked == true)
                        dsbaocao = f_selectdoanhthu_khachhang_chitietso(true, NGAYGIO, date_ngayhethong.DateTime, date_ngayhethong.DateTime, com_khohang._GetselectedID(), false);
                    else
                        dsbaocao = f_selectdoanhthu_khachhang_chitietso(true, NGAYGIO, date_ngay_baocao_z.DateTime, date_ngay_baocao_z.DateTime, com_khohang._GetselectedID(), true);
                    if (dsbaocao.Tables.Count > 0)
                        luoi_doanhthu.DataSource = dsbaocao.Tables[0];
                    txt_kieudoanhthu.Text = " Khách hàng";
                    break;

                case 3:    // bao cao khach hang mat hang
                    str_loaibaocao = "khachhang_mathang";
                    luoi_doanhthu.MainView = grid_khachhang_mathang;
                    luoi_doanhthu.DataSource = null;
                    if (radio_baocaoX.Checked == true)
                        dsbaocao = f_selectdoanhthu_khachhang_mathang(true, NGAYGIO, date_ngayhethong.DateTime, date_ngayhethong.DateTime, com_khohang._GetselectedID(), false);
                    else
                        dsbaocao = f_selectdoanhthu_khachhang_mathang(true, NGAYGIO, date_ngay_baocao_z.DateTime, date_ngay_baocao_z.DateTime, com_khohang._GetselectedID(), true);
                    if (dsbaocao != null)
                    {
                        if (dsbaocao.Tables.Count > 0)

                            luoi_doanhthu.DataSource = dsbaocao.Tables[0];

                        txt_kieudoanhthu.Text = " Khách hàng";

                    }
                    break;

            }

            //      txt_kieudoanhthu.Text = " KHÁCH HÀNG";

        }

        private void luoi_doanhthu_DefaultViewChanged(object sender, EventArgs e)
        {

            switch (luoi_doanhthu.DefaultView.Name)
            {
                case "grid_doanhthu_khachhang":
                    lbel_khachhang.BackColor = System.Drawing.Color.Red;
                    break;

            }
        }
        DataSet dsbaocao;
        private void lbel_khuvuc_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;
            if (!new cpublic().KiemTra_CoQuyen(this.Name, ((System.Windows.Forms.Control)sender).Name, 1))
            {
                MessageBox_LPV.Show("Bạn không có quyền");
                return;
            }
            ngaythang = new xulyngaythangnam();
            Variable.str_tungay = ngaythang.chuoingay(ngaythang.chuyenngay(date_ngayhethong.Text));
            mh_dm_baocaokhuvuc_ban frm_dm_baocaokhuvuc_ban = new mh_dm_baocaokhuvuc_ban();
            frm_dm_baocaokhuvuc_ban.ShowDialog();
            if (frm_dm_baocaokhuvuc_ban.DialogResult == DialogResult.Cancel)
                return;

            if (radio_baocaoX.Checked)
            {
                switch (frm_dm_baocaokhuvuc_ban.str_loaibaocao)
                {
                    case "khuvuc":
                        str_loaibaocao = "khuvuc";
                        luoi_doanhthu.MainView = grid_doanhthu_khuvuc;
                        luoi_doanhthu.DataSource = null;
                        if (radio_baocaoX.Checked == true)
                            dsbaocao = f_selectdoanhthu_khuvuc_ban(1, ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), false);
                        else
                            dsbaocao = f_selectdoanhthu_khuvuc_ban(1, ngaythang.chuyenngay(date_ngay_baocao_z.Text), ngaythang.chuyenngay(date_ngay_baocao_z.Text), com_khohang._GetselectedID(), true);
                        txt_kieudoanhthu.Text = " Khu vực";
                        break;
                    case "ban":
                        str_loaibaocao = "ban";
                        luoi_doanhthu.MainView = grid_danhthu_ban;
                        luoi_doanhthu.DataSource = null;
                        if (radio_baocaoX.Checked == true)
                            dsbaocao = f_selectdoanhthu_khuvuc_ban(2, ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), false);
                        else
                            dsbaocao = f_selectdoanhthu_khuvuc_ban(2, ngaythang.chuyenngay(date_ngay_baocao_z.Text), ngaythang.chuyenngay(date_ngay_baocao_z.Text), com_khohang._GetselectedID(), true);
                        txt_kieudoanhthu.Text = "Bàn";
                        break;
                    case "khuvuc_ct":
                        str_loaibaocao = "khuvuc_ct";
                        luoi_doanhthu.MainView = grid_danhthu_ban;
                        luoi_doanhthu.DataSource = null;
                        if (radio_baocaoX.Checked == true)
                            dsbaocao = f_selectdoanhthu_khuvuc_ban(3, ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), false);
                        else
                            dsbaocao = f_selectdoanhthu_khuvuc_ban(3, ngaythang.chuyenngay(date_ngay_baocao_z.Text), ngaythang.chuyenngay(date_ngay_baocao_z.Text), com_khohang._GetselectedID(), true);
                        txt_kieudoanhthu.Text = "Khu vực chi tiết";
                        break;

                        
                }

                if (dsbaocao.Tables[0].Rows.Count > 0)// && (dsbaocao.Tables[0].Rows[0]["THANHTIENTRUOC"] + "") != "")
                {
                    //--------bc theo hinh thuc thanh toan,-- editor: PL, date: 2012.07.25
                    dtienmat = f_Tongtienmat(ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                    decimal.TryParse(dtienmat.Rows[0]["TONGTIEN"] + "", out sumTienmat);

                    dThe = f_TongThetratruoc(ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                    decimal.TryParse(dThe.Rows[0]["TONGTIEN"] + "", out sumThe);
                    ////////--------------- 

                    luoi_doanhthu.DataSource = dsbaocao.Tables[1];
                    f_chitietdoanhthu(ds);
                }
                else
                    f_xoachitietdoanhthu();
            }

        }

        private void lbel_thoat_Click(object sender, EventArgs e)
        {

            this.Close();
        }

        private void lbel_inbaocao_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;
            if (!new cpublic().KiemTra_CoQuyen(this.Name, ((System.Windows.Forms.Control)sender).Name, 1))
            {
                MessageBox_LPV.Show("Bạn không có quyền");
                return;
            }
            //=========================================IN BAO CAO TAT CA, DOANH THU, HIEN HANH ===================================================
            mh_inbaocao frm_inbillbaocao = new mh_inbaocao();
            frm_inbillbaocao.ShowDialog();

            if (frm_inbillbaocao.DialogResult != DialogResult.OK)
                return;
            switch (frm_inbillbaocao.loaibaocao)
            {

                case 1:
                    mh_tungaydenngay frm_tungaydenngay = new mh_tungaydenngay();
                    frm_tungaydenngay.ShowDialog();

                    if (frm_tungaydenngay.DialogResult != DialogResult.OK)
                        return;
                    if (int.Parse(frm_tungaydenngay.str_fromdate) > int.Parse(frm_tungaydenngay.str_todate))
                        return;

                    //if (radio_baocaoX.Checked == true)
                    //    ds = f_selectdoanhthu_tatca(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), false);
                    //else
                    //    ds = f_selectdoanhthu_tatca(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), true);
                   //---------In tất cả-------------26/06/2012------------------------
                    ds = f_selectdoanhthu_tatca(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);

                    //--------bc theo hinh thuc thanh toan,-- editor: PL, date: 2012.07.25
                    dtienmat = f_Tongtienmat(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                    decimal.TryParse(dtienmat.Rows[0]["TONGTIEN"] + "", out sumTienmat);

                    dThe = f_TongThetratruoc(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                    decimal.TryParse(dThe.Rows[0]["TONGTIEN"] + "", out sumThe);
                    // end

                    Mod_inbill_tatcabaocao inbill_tatcabaocao = new Mod_inbill_tatcabaocao();
                    inbill_tatcabaocao.f_baocaodoanhthu_tatca(ds, frm_tungaydenngay.date_tungay.DateTime.ToString("dd/MM/yyyy"), frm_tungaydenngay.date_denngay.DateTime.ToString("dd/MM/yyyy"), sumTienmat, sumThe); // bc httt, editor: PL, date: 2012.08.10
                    break;

                case 2:
                    Mod_baocaodoanhthu_tatca_in doanhthutatca_in = new Mod_baocaodoanhthu_tatca_in();
                    //----------In doanh thu---------------26/06/2012-----------------------
                    doanhthutatca_in.f_baocaodoanhthu_tatca_in(lbel_tongcongchuatanggiam.Text, lbel_giamgia_1.Text,
                                               lbel_vat_2.Text, lbel_thuedichvu_2.Text, lbel_tientip_2.Text, lbel_tongcongdagiamgia.Text,
                                               lbel_datanggiam.Text, lbel_phuthu_2.Text, lbel_tienthu_2.Text, lbel_tienchi_2.Text, lbel_congno.Text, lbel_khuyenmai.Text, lbel_datcoc.Text, lbel_datcoc_tru.Text, lbl_Tienmat.Text, lbl_The.Text); // bc httt, editor: PL, date: 2012.08.10
                                           //-------Thêm đặt cọc và trừ đặt cọc----26/06/2012-----------------

                    break;

                case 3:                  // ============= in bao cao doanh thu hien hanh
                    
                    Mod_baocaodoanhthu_hienhanh_in doanhthuhienhanh_in = new Mod_baocaodoanhthu_hienhanh_in();

                    doanhthuhienhanh_in.f_baocaodoanhthu_hienhanh_in(str_loaibaocao, dsbaocao, dt_bcthe); // bc httt, editor: PL, date: 2012.08.10
                    
                    break;
            }

        }

        private int f_baocaodoanhthu(ReportDocument CrReport)
        {
            try
            {

                Mod_baocao bc = new Mod_baocao();
                bc.f_header_report(CrReport);

                CrystalDecisions.CrystalReports.Engine.TextObject str_ngaythangnam;
                str_ngaythangnam = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_tungaydenngay"];
                if (check_ngay.Checked == true)
                    str_ngaythangnam.Text = Variable.str_tungay;
                else
                    str_ngaythangnam.Text = "";

                CrystalDecisions.CrystalReports.Engine.TextObject str_tongcongchuatanggiam;
                str_tongcongchuatanggiam = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_tongcongchuatanggiam"];
                str_tongcongchuatanggiam.Text = lbel_tongcongchuatanggiam.Text;

                CrystalDecisions.CrystalReports.Engine.TextObject str_giamgia;
                str_giamgia = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_giamgiam"];
                str_giamgia.Text = lbel_giamgia_1.Text;

                CrystalDecisions.CrystalReports.Engine.TextObject str_thuevat;
                str_thuevat = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_thuevat"];
                str_thuevat.Text = lbel_vat_1.Text;

                CrystalDecisions.CrystalReports.Engine.TextObject str_thuedichvu;
                str_thuedichvu = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_thuedichvu"];
                str_thuedichvu.Text = lbel_thuedichvu_1.Text;

                CrystalDecisions.CrystalReports.Engine.TextObject str_tientip;
                str_tientip = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_tientip"];
                str_tientip.Text = lbel_tientip_1.Text;

                CrystalDecisions.CrystalReports.Engine.TextObject str_tongcongdatanggiam;
                str_tongcongdatanggiam = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_tongcongdatanggiam"];
                str_tongcongdatanggiam.Text = lbel_tongcongdagiamgia.Text;

                CrystalDecisions.CrystalReports.Engine.TextObject str_tientrongket;
                str_tientrongket = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_tientrongket"];
                str_tientrongket.Text = lbel_tientrongket.Text;

                return 1;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        private int f_convert_reporttopdf(ReportDocument CrReport, string path_filename)
        {
            try
            {

                ExportOptions CrExportOptions = null;

                DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                ExcelFormatOptions CrFormatTypeOptions = new ExcelFormatOptions();
                CrDiskFileDestinationOptions.DiskFileName = path_filename;
                CrExportOptions = CrReport.ExportOptions;
                CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                CrExportOptions.ExportFormatType = ExportFormatType.Excel;
                CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                CrExportOptions.FormatOptions = CrFormatTypeOptions;
                CrReport.Export();


                return 1;
            }
            catch (Exception)
            {
                return 0;
            }

        }

        private int f_showdataonsubreport(ReportDocument CrReport, Boolean loaibaocao, Boolean gio, DateTime date_tungay, DateTime date_denngay, string tungay, string denngay, string makho)
        {
            try
            {
                ds = f_selecttatcacacdoanhthu(loaibaocao, gio, date_tungay, date_denngay, tungay, denngay, makho);
                if (ds.Tables[0].Rows[0]["THANHTIENTRUOC"] + "" == "") //     neu khong co du lieu
                {
                    return 0;
                }
                // BAO CAO DOANH THU
                //----------------------------
                //------------------------------------
                CrystalDecisions.CrystalReports.Engine.TextObject str_ngaythangnam;
                str_ngaythangnam = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_tungaydenngay"];
                str_ngaythangnam.Text = Variable.str_tungay;

                //-----------------------------------------------------------------------------------------------------------------------------------------------------------
                CrystalDecisions.CrystalReports.Engine.TextObject str_tongcongchuatanggiam;
                str_tongcongchuatanggiam = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_tongcongchuatanggiam"];
                str_tongcongchuatanggiam.Text = ds.Tables[0].Rows[0]["THANHTIENTRUOC"] + "";

                CrystalDecisions.CrystalReports.Engine.TextObject str_giamgia;
                str_giamgia = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_giamgiam"];
                str_giamgia.Text = ds.Tables[0].Rows[0]["CHIECKHAU"] + "";

                CrystalDecisions.CrystalReports.Engine.TextObject str_thuevat;
                str_thuevat = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_thuevat"];
                str_thuevat.Text = ds.Tables[0].Rows[0]["Vat"] + "";

                CrystalDecisions.CrystalReports.Engine.TextObject str_thuedichvu;
                str_thuedichvu = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_thuedichvu"];
                str_thuedichvu.Text = ds.Tables[0].Rows[0]["SERVICE_CHARGE"] + "";

                CrystalDecisions.CrystalReports.Engine.TextObject str_tientip;
                str_tientip = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_tientip"];
                str_tientip.Text = ds.Tables[0].Rows[0]["TIENTIP"] + "";

                CrystalDecisions.CrystalReports.Engine.TextObject str_tongcongdatanggiam;
                str_tongcongdatanggiam = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_tongcongdatanggiam"];
                str_tongcongdatanggiam.Text = ds.Tables[0].Rows[0]["THANHTIEN"] + "";

                CrystalDecisions.CrystalReports.Engine.TextObject str_tientrongket;
                str_tientrongket = (CrystalDecisions.CrystalReports.Engine.TextObject)CrReport.ReportDefinition.ReportObjects["txt_tientrongket"];
                str_tientrongket.Text = ds.Tables[0].Rows[0]["THANHTIEN"] + "";

                //--------------------------

                // DOI VOI NHOM CHU HANG HOA
                CrReport.Subreports[1].SetDataSource(ds.Tables[1]);
                CrReport.Subreports[1].DataDefinition.FormulaFields["NHOMCHU"].Text = "{dt_report.NHOMCHU}" + "";
                CrReport.Subreports[1].DataDefinition.FormulaFields["THANHTIEN"].Text = "{dt_report.THANHTIENTRUOC}" + "";
                CrReport.Subreports[1].DataDefinition.FormulaFields["CHENHLECH"].Text = "{dt_report.CHENHLECH}" + "";
                CrReport.Subreports[1].DataDefinition.FormulaFields["TIENMAT"].Text = "{dt_report.TIENMAT}" + "";
                CrReport.Subreports[1].DataDefinition.FormulaFields["TYLE"].Text = "{dt_report.TYLE}" + "";
                // DOI VOI NHOM HANG
                CrReport.Subreports[0].SetDataSource(ds.Tables[2]);
                CrReport.Subreports[0].DataDefinition.FormulaFields["NHOMHANG"].Text = "{dt_report.NHOMHANG}" + "";
                CrReport.Subreports[0].DataDefinition.FormulaFields["THANHTIEN"].Text = "{dt_report.THANHTIENTRUOC}" + "";
                CrReport.Subreports[0].DataDefinition.FormulaFields["CHENHLECH"].Text = "{dt_report.CHENHLECH}" + "";
                CrReport.Subreports[0].DataDefinition.FormulaFields["TIENMAT"].Text = "{dt_report.TIENMAT}" + "";
                CrReport.Subreports[0].DataDefinition.FormulaFields["TYLE"].Text = "{dt_report.TYLE}" + "";
                // DOI VOI NHAN VIEN
                CrReport.Subreports[2].SetDataSource(ds.Tables[3]);
                CrReport.Subreports[2].DataDefinition.FormulaFields["MANV"].Text = "{dt_report.MANV}" + "";
                CrReport.Subreports[2].DataDefinition.FormulaFields["TENNV"].Text = "{dt_report.TENNV}" + "";
                CrReport.Subreports[2].DataDefinition.FormulaFields["THANHTIEN"].Text = "{dt_report.THANHTIENTRUOC}" + "";
                CrReport.Subreports[2].DataDefinition.FormulaFields["CHENHLECH"].Text = "{dt_report.CHENHLECH}" + "";
                CrReport.Subreports[2].DataDefinition.FormulaFields["TIENMAT"].Text = "{dt_report.TIENMAT}" + "";
                CrReport.Subreports[2].DataDefinition.FormulaFields["TYLE"].Text = "{dt_report.TYLE}" + "";
                // DOI VOI KHACH HANG
                CrReport.Subreports[3].SetDataSource(ds.Tables[4]);
                CrReport.Subreports[3].DataDefinition.FormulaFields["MAKH"].Text = "{dt_report.MAKH}" + "";
                CrReport.Subreports[3].DataDefinition.FormulaFields["TENKH"].Text = "{dt_report.TENKH}" + "";
                CrReport.Subreports[3].DataDefinition.FormulaFields["THANHTIEN"].Text = "{dt_report.THANHTIENTRUOC}" + "";
                CrReport.Subreports[3].DataDefinition.FormulaFields["CHENHLECH"].Text = "{dt_report.CHENHLECH}" + "";
                CrReport.Subreports[3].DataDefinition.FormulaFields["TIENMAT"].Text = "{dt_report.TIENMAT}" + "";
                CrReport.Subreports[3].DataDefinition.FormulaFields["TYLE"].Text = "{dt_report.TYLE}" + "";
                // DOI VOI KHU VUC
                CrReport.Subreports[4].SetDataSource(ds.Tables[6]);
                CrReport.Subreports[4].DataDefinition.FormulaFields["KHUVUC"].Text = "{dt_report.KHUVUC}" + "";
                //     CrReport.Subreports[4].DataDefinition.FormulaFields["TENBAN"].Text = "{dt_report.BAN}" + "";
                CrReport.Subreports[4].DataDefinition.FormulaFields["THANHTIEN"].Text = "{dt_report.THANHTIENTRUOC}" + "";
                CrReport.Subreports[4].DataDefinition.FormulaFields["CHENHLECH"].Text = "{dt_report.CHENHLECH}" + "";
                CrReport.Subreports[4].DataDefinition.FormulaFields["TIENMAT"].Text = "{dt_report.TIENMAT}" + "";
                CrReport.Subreports[4].DataDefinition.FormulaFields["TYLE"].Text = "{dt_report.TYLE}" + "";

                // DOI VOI KHO HANG
                CrReport.Subreports[5].SetDataSource(ds.Tables[5]);
                CrReport.Subreports[5].DataDefinition.FormulaFields["KHOHANG"].Text = "{dt_report.KHOHANG}" + "";
                CrReport.Subreports[5].DataDefinition.FormulaFields["THANHTIEN"].Text = "{dt_report.THANHTIENTRUOC}" + "";
                CrReport.Subreports[5].DataDefinition.FormulaFields["CHENHLECH"].Text = "{dt_report.CHENHLECH}" + "";
                CrReport.Subreports[5].DataDefinition.FormulaFields["TIENMAT"].Text = "{dt_report.TIENMAT}" + "";
                CrReport.Subreports[5].DataDefinition.FormulaFields["TYLE"].Text = "{dt_report.TYLE}" + "";

                // DOI VOI HANG HOA
                CrReport.Subreports[6].SetDataSource(ds.Tables[7]);
                CrReport.Subreports[6].DataDefinition.FormulaFields["MAHH"].Text = "{dt_report.MAHH}" + "";
                CrReport.Subreports[6].DataDefinition.FormulaFields["TENHH"].Text = "{dt_report.HANGHOA}" + "";
                CrReport.Subreports[6].DataDefinition.FormulaFields["THANHTIEN"].Text = "{dt_report.THANHTIENTRUOC}" + "";
                CrReport.Subreports[6].DataDefinition.FormulaFields["CHENHLECH"].Text = "{dt_report.CHENHLECH}" + "";
                CrReport.Subreports[6].DataDefinition.FormulaFields["TIENMAT"].Text = "{dt_report.TIENMAT}" + "";
                CrReport.Subreports[6].DataDefinition.FormulaFields["SOLUONG"].Text = "{dt_report.SOLUONG}" + "";
                CrReport.Subreports[6].DataDefinition.FormulaFields["TYLE"].Text = "{dt_report.TYLE}" + "";

                // DOI VOI BAN
                CrReport.Subreports[7].SetDataSource(ds.Tables[8]);
                CrReport.Subreports[7].DataDefinition.FormulaFields["KHUVUC"].Text = "{dt_report.KHUVUC}" + "";
                CrReport.Subreports[7].DataDefinition.FormulaFields["BAN"].Text = "{dt_report.TENBAN}" + "";
                CrReport.Subreports[7].DataDefinition.FormulaFields["THANHTIEN"].Text = "{dt_report.THANHTIENTRUOC}" + "";
                CrReport.Subreports[7].DataDefinition.FormulaFields["CHENHLECH"].Text = "{dt_report.CHENHLECH}" + "";
                CrReport.Subreports[7].DataDefinition.FormulaFields["TIENMAT"].Text = "{dt_report.TIENMAT}" + "";
                CrReport.Subreports[7].DataDefinition.FormulaFields["TYLE"].Text = "{dt_report.TYLE}" + "";
                return 1;
            }
            catch (Exception)
            {

                return 0;
            }
        }

        private void lbel_fileexcel_Click_1(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;
            if (!new cpublic().KiemTra_CoQuyen(this.Name, ((System.Windows.Forms.Control)sender).Name, 1))
            {
                MessageBox_LPV.Show("Bạn không có quyền");
                return;
            }
            mh_dmbaocao frm_dmbaocao = new mh_dmbaocao(dsbaocao);
            frm_dmbaocao.ShowDialog();

            if (frm_dmbaocao.DialogResult != DialogResult.OK)
                return;

            Mod_header_report header_report = new Mod_header_report();
            Boolean hienthigio = false;
            Boolean loaibaocao = false;
            int ketqua = 0;

            mh_save frm_save;
            ReportDocument CrReport;
            DataSet ds = null;
            xulyngaythangnam ngaythang = new xulyngaythangnam();
            switch (frm_dmbaocao.str_loaibaocao)
            {
                case 1:                  // bao cao tat ca cac doanh thu

                    mh_tungaydenngay frm_tungaydenngay = new mh_tungaydenngay();
                    frm_tungaydenngay.ShowDialog();

                    if (frm_tungaydenngay.DialogResult != DialogResult.OK)
                        return;
                    if (int.Parse(frm_tungaydenngay.str_fromdate) > int.Parse(frm_tungaydenngay.str_todate))
                        return;
                    string sTenFile = "tatca_doanhthu_" + DateTime.Now.ToString("yyMMdd_HHmmss") + ".xls";
                    frm_save = new mh_save(sTenFile);
                    frm_save.ShowDialog();

                    if (frm_save.path_excel == "")
                        return;
                    //aaaa
                    //  DataSet ds = f_selectdoanhthu_tatca(true, date_ngayhethong.DateTime, date_ngayhethong.DateTime, date_ngayhethong.DateTime, com_khohang._GetselectedID(), radio_baocaoZ.Checked);


                    //if (radio_baocaoX.Checked == true)
                    //    ds = f_selectdoanhthu_tatca(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), false);
                    //else
                    //    ds = f_selectdoanhthu_tatca(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), true);
                    ds = f_selectdoanhthu_tatca(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);


                    //--------bc theo hinh thuc thanh toan,-- editor: PL, date: 2012.07.25
                    dtienmat = f_Tongtienmat(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                    decimal.TryParse(dtienmat.Rows[0]["TONGTIEN"] + "", out sumTienmat);

                    dThe = f_TongThetratruoc(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                    decimal.TryParse(dThe.Rows[0]["TONGTIEN"] + "", out sumThe);
                    ////////--------------- 


                    Mod_baocaodoanhthu_tatca doanhthu_tatca = new Mod_baocaodoanhthu_tatca();
                    doanhthu_tatca.rpt_baocaodoanhthu_tatca(ds, frm_save.path_excel, lbel_tongcongchuatanggiam.Text, lbel_giamgia_1.Text,
                                               lbel_vat_1.Text, lbel_thuedichvu_1.Text, lbel_tientip_1.Text, lbel_tongcongdagiamgia.Text, lbel_datanggiam.Text, sumTienmat, sumThe); // bc httt, editor: PL, date: 2012.07.25

                    //ds.Tables[0].TableName = "Báo cáo tổng hợp";

                    //new cpublic().ExportExcel(ds, frm_save.path_excel);
                    break;

                case 2:                  // bao cao doanh thu tong quat
                     sTenFile = "baocao_doanhthu_" + DateTime.Now.ToString("yyMMdd_HHmmss") + ".xls";
                    frm_save = new mh_save(sTenFile);                    
                    frm_save.ShowDialog();
                    if (frm_save.path_excel == "")
                        return;
                    Mod_baocaodoanhthu_tongquat baocaodoanhthu_tongquat = new Mod_baocaodoanhthu_tongquat();
                    baocaodoanhthu_tongquat.rpt_baocaodoanhthu_tongquat(frm_save.path_excel.Replace("//", "\\"), lbel_tongcongchuatanggiam.Text, lbel_giamgia_2.Text,
                                                            lbel_vat_2.Text, lbel_thuedichvu_2.Text, lbel_tientip_2.Text, lbel_tongcongdagiamgia.Text, lbel_datanggiam.Text,
                                                            lbel_phuthu_2.Text, lbel_tienthu_2.Text, lbel_tienchi_2.Text, lbel_congno.Text, lbel_khuyenmai.Text, SumTienmatThuchi, sumThe); //bc httt, editor: PL, date: 2012.07.25

                    break;

                case 3:                  // bao cao doanh thu hien hanh

                    frm_tungaydenngay = new mh_tungaydenngay();
                    frm_tungaydenngay.ShowDialog();

                    if (frm_tungaydenngay.DialogResult != DialogResult.OK)
                        return;
                    if (int.Parse(frm_tungaydenngay.str_fromdate) > int.Parse(frm_tungaydenngay.str_todate))
                        return;
                    //sTenFile = "tatca_doanhthu_hienhanh" + DateTime.Now.ToString("yyMMdd_HHmmss") + ".xls";
                    //frm_save = new mh_save(sTenFile);
                    //frm_save.ShowDialog();

                    //if (frm_save.path_excel == "")
                    //    return;


                    dt_bcthe = f_bcThe(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked); //bc httt, editor: PL, date: 2012.07.25

                    Mod_baocaodoanhthu_hienhanh doanhthuhienhanh = new Mod_baocaodoanhthu_hienhanh();
                    doanhthuhienhanh.f_baocaodoanhthu_hienhanh(str_loaibaocao, dsbaocao, luoi_doanhthu, dt_bcthe); //bc httt, editor: PL, date: 2012.07.25

                    break;
            }

        }

        private void lbel_doanhthu_theogio_Click(object sender, EventArgs e)
        {
            if (!new cpublic().KiemTra_CoQuyen(this.Name, ((System.Windows.Forms.Control)sender).Name, 1))
            {
                MessageBox_LPV.Show("Bạn không có quyền");
                return;
            }
            //if (radio_baocaoX.Checked)
            //{
            str_loaibaocao = "baocaotheogio";
            mh_list_gio omh_list_gio = new mh_list_gio();
            sp = omh_list_gio.ShowForm();
            Variable.str_tungay = "Ngay " + date_ngayhethong.Text;

            DateTime NGAYGIO = new DateTime(date_ngayhethong.DateTime.Year, date_ngayhethong.DateTime.Month, date_ngayhethong.DateTime.Day, sp.Hours, sp.Minutes, 0);
            if (radio_baocaoZ.Checked)
                NGAYGIO = new DateTime(date_ngay_baocao_z.DateTime.Year, date_ngay_baocao_z.DateTime.Month, date_ngay_baocao_z.DateTime.Day, sp.Hours, sp.Minutes, 0);

            luoi_doanhthu.MainView = grid_danhthu_gio;
            luoi_doanhthu.DataSource = null;

            if (radio_baocaoX.Checked == true)
                dsbaocao = f_selectdoanhthu_gio(true, NGAYGIO, date_ngayhethong.DateTime, date_ngayhethong.DateTime, com_khohang._GetselectedID(), false);
            else
                dsbaocao = f_selectdoanhthu_gio(true, NGAYGIO, date_ngay_baocao_z.DateTime, date_ngay_baocao_z.DateTime, com_khohang._GetselectedID(), true);

            luoi_doanhthu.DataSource = dsbaocao.Tables[1];
            txt_kieudoanhthu.Text = "Báo cáo doanh thu theo giờ";

            if (ds.Tables[0].Rows.Count > 0)// && (ds.Tables[0].Rows[0]["THANHTIENTRUOC"] + "") != "")
            {
                //--------bc theo hinh thuc thanh toan,-- editor: PL, date: 2012.07.25
                dtienmat = f_Tongtienmat(date_ngayhethong.DateTime.ToString(), date_ngayhethong.DateTime.ToString(), com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                decimal.TryParse(dtienmat.Rows[0]["TONGTIEN"] + "", out sumTienmat);

                dThe = f_TongThetratruoc(date_ngayhethong.DateTime.ToString(), date_ngayhethong.DateTime.ToString(), com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                decimal.TryParse(dThe.Rows[0]["TONGTIEN"] + "", out sumThe);
                ////////--------------- 

                f_chitietdoanhthu(ds);
            }
            else
                f_xoachitietdoanhthu();


            // }
        }

        private void check_hiengio_CheckedChanged(object sender, EventArgs e)
        {
            if (check_hiengio.Checked == true)
            {
                date_ngay_baocao_z.Properties.DisplayFormat.FormatString = "dd/MM/yyyy HH:mm";
                date_ngay_baocao_z.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                date_ngay_baocao_z.Properties.EditFormat.FormatString = "dd/MM/yyyy HH:mm";
                date_ngay_baocao_z.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                date_ngay_baocao_z.Properties.EditMask = "dd/MM/yyyy HH:mm";

                date_ngayhethong.Properties.DisplayFormat.FormatString = "dd/MM/yyyy HH:mm";
                date_ngayhethong.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                date_ngayhethong.Properties.EditFormat.FormatString = "dd/MM/yyyy HH:mm";
                date_ngayhethong.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                date_ngayhethong.Properties.EditMask = "dd/MM/yyyy HH:mm";
                //date_tungay.DateTime = DateTime.Now;
                //date_denngay.DateTime = DateTime.Now;
                date_ngay_baocao_z.DateTime = new DateTime(date_ngay_baocao_z.DateTime.Year, date_ngay_baocao_z.DateTime.Month, date_ngay_baocao_z.DateTime.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                date_ngayhethong.DateTime = new DateTime(date_ngayhethong.DateTime.Year, date_ngayhethong.DateTime.Month, date_ngayhethong.DateTime.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
            }
            else
            {
                date_ngay_baocao_z.Properties.DisplayFormat.FormatString = "dd/MM/yyyy";
                date_ngay_baocao_z.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                date_ngay_baocao_z.Properties.EditFormat.FormatString = "dd/MM/yyyy";
                date_ngay_baocao_z.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                date_ngay_baocao_z.Properties.EditMask = "dd/MM/yyyy";

                date_ngayhethong.Properties.DisplayFormat.FormatString = "dd/MM/yyyy";
                date_ngayhethong.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                date_ngayhethong.Properties.EditFormat.FormatString = "dd/MM/yyyy";
                date_ngayhethong.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                date_ngayhethong.Properties.EditMask = "dd/MM/yyyy";

                date_ngay_baocao_z.DateTime = DateTime.Today;
                date_ngayhethong.DateTime = DateTime.Today;
            }

        }

        private void date_tungay_Click(object sender, EventArgs e)
        {
            //string date = new cpublic().loadformso_point(550, 180);
            //if (date != "")
            //    date_tungay.SelectedText = date;

        }

        private void date_denngay_Click(object sender, EventArgs e)
        {
            //string date = new cpublic().loadformso_point(830, 180);
            //if (date != "")
            //    date_denngay.SelectedText = date;

        }

        private void bton_xoatatca_Click(object sender, EventArgs e)
        {
            if (!new cpublic().KiemTra_CoQuyen(this.Name, ((System.Windows.Forms.Control)sender).Name, 1))
            {
                MessageBox_LPV.Show("Bạn không có quyền");
                return;
            }
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Xóa tất cả báo cáo?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {
                int ketqua = f_deletephieubanhang(com_khohang._GetselectedID(), radio_baocaoX.Checked);

                if (ketqua <= -1)
                    mess.messagebox_pos("Thông báo", "Xóa không thành công", loaithongbao.thongtin.GetHashCode());
                else
                {
                    mess.messagebox_pos("Thông báo", "Xóa thành công", loaithongbao.thongtin.GetHashCode());
                    luoi_doanhthu.DataSource = null;
                    f_xoachitietdoanhthu();
                }
            }
        }

        private void bton_tungaydenngay_Click(object sender, EventArgs e)
        {

            mh_tungaydenngay frm_tungaydenngay = new mh_tungaydenngay();
            frm_tungaydenngay.ShowDialog();

            if (frm_tungaydenngay.DialogResult != DialogResult.OK)
                return;
            if (int.Parse(frm_tungaydenngay.str_fromdate) > int.Parse(frm_tungaydenngay.str_todate))
                return;
            xulyngaythangnam ngaythang = new xulyngaythangnam();
            Variable.str_tungay = ngaythang.chuoi_tungay_denngay(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate);

            //--------bc theo hinh thuc thanh toan,-- editor: PL, date: 2012.07.25
            dtienmat = f_Tongtienmat(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);
            decimal.TryParse(dtienmat.Rows[0]["TONGTIEN"] + "", out sumTienmat);

            dThe = f_TongThetratruoc(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);
            decimal.TryParse(dThe.Rows[0]["TONGTIEN"] + "", out sumThe);
            ////////--------------- 

            switch (str_loaibaocao)
            {
                case "nhomchu":
                    str_loaibaocao = "nhomchu";
                    luoi_doanhthu.MainView = grid_doanhthu_nhomchu;
                    luoi_doanhthu.DataSource = null;
                    //if (radio_baocaoX.Checked == true)
                    //    dsbaocao = f_selectdoanhthu_nhom(1, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), false);
                    //else
                    //    dsbaocao = f_selectdoanhthu_nhom(1, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), true);
                    dsbaocao = f_selectdoanhthu_nhom(1, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);

                    //luoi_doanhthu.DataSource = dsbaocao.Tables[1];
                    if (dsbaocao.Tables[0].Rows.Count > 0)// && (dsbaocao.Tables[0].Rows[0]["THANHTIENTRUOC"] + "") != "")
                    {
                        luoi_doanhthu.DataSource = dsbaocao.Tables[1];
                        f_chitietdoanhthu(ds);
                    }
                    else
                        f_xoachitietdoanhthu();

                    txt_kieudoanhthu.Text = " Nhóm chủ";
                    break;

                //---- bc doanh thu theo the, --- editor: PL, date: 2012.07.30
                case "thethanhtoan":
                    str_loaibaocao = "thethanhtoan";
                    luoi_doanhthu.MainView = grv_bcTheTT;
                    luoi_doanhthu.DataSource = null;

                    ds = f_selectdoanhthu_nhom(1, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                    dt_bcthe = f_bcThe(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                    if (dt_bcthe.Rows.Count > 0)
                    {
                        luoi_doanhthu.DataSource = dt_bcthe;
                        if(ds.Tables[1].Rows.Count > 0)
                            f_chitietdoanhthu(ds);
                    }
                    txt_kieudoanhthu.Text = " Các loại thẻ thanh toán";
                    break;
                //end
                case "nhomhang":
                    str_loaibaocao = "nhomhang";
                    luoi_doanhthu.MainView = grid_doanhthu_nhomhang;
                    luoi_doanhthu.DataSource = null;
                    //if (radio_baocaoX.Checked == true)
                    //    dsbaocao = f_selectdoanhthu_nhom(2, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), false);
                    //else
                    //    dsbaocao = f_selectdoanhthu_nhom(2, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), true);
                    dsbaocao = f_selectdoanhthu_nhom(2, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);

                    //luoi_doanhthu.DataSource = dsbaocao.Tables[1];
                    if (dsbaocao.Tables[0].Rows.Count > 0)// && (dsbaocao.Tables[0].Rows[0]["THANHTIENTRUOC"] + "") != "")
                    {
                        luoi_doanhthu.DataSource = dsbaocao.Tables[1];
                        f_chitietdoanhthu(ds);
                    }
                    else
                        f_xoachitietdoanhthu();

                    txt_kieudoanhthu.Text = " Nhóm hàng";
                    break;
                case "mathang":
                    str_loaibaocao = "mathang";
                    luoi_doanhthu.MainView = grid_doanhthu_hanghoa;
                    luoi_doanhthu.DataSource = null;
                    //if (radio_baocaoX.Checked == true)
                    //    dsbaocao = f_selectdoanhthu_nhom(3, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), false);
                    //else
                    //    dsbaocao = f_selectdoanhthu_nhom(3, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), true);
                    dsbaocao = f_selectdoanhthu_nhom(3, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                    //luoi_doanhthu.DataSource = dsbaocao.Tables[1];
                    if (dsbaocao.Tables[0].Rows.Count > 0)// && (dsbaocao.Tables[0].Rows[0]["THANHTIENTRUOC"] + "") != "")
                    {
                        luoi_doanhthu.DataSource = dsbaocao.Tables[1];
                        f_chitietdoanhthu(ds);
                    }
                    else
                        f_xoachitietdoanhthu();

                    txt_kieudoanhthu.Text = " Mặt hàng";
                    break;

                case "baocaotheogio":
                    str_loaibaocao = "baocaotheogio";
                    luoi_doanhthu.MainView = grid_danhthu_gio;
                    luoi_doanhthu.DataSource = null;
                    DateTime NGAYGIO = new DateTime(date_ngayhethong.DateTime.Year, date_ngayhethong.DateTime.Month, date_ngayhethong.DateTime.Day, sp.Hours, sp.Minutes, 0);
                    //DateTime NGAYGIO = new DateTime(date_ngayhethong.DateTime.Year, date_ngayhethong.DateTime.Month, date_ngayhethong.DateTime.Day, sp.Hours, sp.Minutes, 0);
                    if (radio_baocaoZ.Checked)
                        NGAYGIO = new DateTime(date_ngay_baocao_z.DateTime.Year, date_ngay_baocao_z.DateTime.Month, date_ngay_baocao_z.DateTime.Day, sp.Hours, sp.Minutes, 0);
                    dsbaocao = f_selectdoanhthu_gio(false, NGAYGIO, frm_tungaydenngay.date_fromdatetime, frm_tungaydenngay.date_todatetime, com_khohang._GetselectedID(), radio_baocaoZ.Checked);

                    //luoi_doanhthu.DataSource = dsbaocao.Tables[1];
                    if (dsbaocao.Tables[0].Rows.Count > 0)// && (dsbaocao.Tables[0].Rows[0]["THANHTIENTRUOC"] + "") != "")
                    {
                        luoi_doanhthu.DataSource = dsbaocao.Tables[1];
                        f_chitietdoanhthu(ds);
                    }
                    else
                        f_xoachitietdoanhthu();

                    txt_kieudoanhthu.Text = " Giờ";
                    break;

                case "khuvuc":
                    str_loaibaocao = "khuvuc";
                    luoi_doanhthu.MainView = grid_doanhthu_khuvuc;
                    luoi_doanhthu.DataSource = null;
                    //if (radio_baocaoX.Checked == true)
                    //    dsbaocao = f_selectdoanhthu_khuvuc_ban(1, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), false);
                    //else
                    //    dsbaocao = f_selectdoanhthu_khuvuc_ban(1, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), true);
                    dsbaocao = f_selectdoanhthu_khuvuc_ban(1, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                    // luoi_doanhthu.DataSource = dsbaocao.Tables[1];

                    if (dsbaocao.Tables[0].Rows.Count > 0)// && (dsbaocao.Tables[0].Rows[0]["THANHTIENTRUOC"] + "") != "")
                    {
                        luoi_doanhthu.DataSource = dsbaocao.Tables[1];
                        f_chitietdoanhthu(ds);
                    }
                    else
                        f_xoachitietdoanhthu();

                    txt_kieudoanhthu.Text = " Khu vực";
                    break;

                case "ban":
                    str_loaibaocao = "ban";
                    luoi_doanhthu.MainView = grid_danhthu_ban;
                    luoi_doanhthu.DataSource = null;
                    //if (radio_baocaoX.Checked == true)
                    //    dsbaocao = f_selectdoanhthu_khuvuc_ban(2, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), false);
                    //else
                    //    dsbaocao = f_selectdoanhthu_khuvuc_ban(2, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), true);
                    dsbaocao = f_selectdoanhthu_khuvuc_ban(2, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);

                    if (dsbaocao.Tables[0].Rows.Count > 0)// && (dsbaocao.Tables[0].Rows[0]["THANHTIENTRUOC"] + "") != "")
                    {
                        luoi_doanhthu.DataSource = dsbaocao.Tables[1];
                        f_chitietdoanhthu(ds);
                    }
                    else
                        f_xoachitietdoanhthu();

                    txt_kieudoanhthu.Text = "Bàn";
                    break;

                case "khuvuc_ct":                    
                    luoi_doanhthu.MainView = grid_danhthu_ban;
                    luoi_doanhthu.DataSource = null;
                    
                    dsbaocao = f_selectdoanhthu_khuvuc_ban(3, frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), !radio_baocaoX.Checked);

                    if (dsbaocao.Tables[0].Rows.Count > 0)// && (dsbaocao.Tables[0].Rows[0]["THANHTIENTRUOC"] + "") != "")
                    {
                        luoi_doanhthu.DataSource = dsbaocao.Tables[1];
                        f_chitietdoanhthu(ds);
                    }
                    else
                        f_xoachitietdoanhthu();

                    txt_kieudoanhthu.Text = "Khu vực chi tiết";
                    break;

                case "nv_tongquat":
                    str_loaibaocao = "nv_tongquat";
                    luoi_doanhthu.MainView = grid_doanhthu_nhanvien_tongquat;
                    luoi_doanhthu.DataSource = null;
                    dsbaocao = f_selectdoanhthu_nhanvien_tongquat(false, date_ngayhethong.DateTime, frm_tungaydenngay.date_fromdatetime, frm_tungaydenngay.date_todatetime, com_khohang._GetselectedID(), radio_baocaoZ.Checked);
                    luoi_doanhthu.DataSource = dsbaocao.Tables[0];
                    txt_kieudoanhthu.Text = " Nhân viên tổng quát";
                    break;

                case "nv_chitietvaora":
                    str_loaibaocao = "nv_chitietvaora";
                    luoi_doanhthu.MainView = grid_doanhthu_nhanvien_chitietvaora;
                    luoi_doanhthu.DataSource = null;
                    dsbaocao = f_selectdoanhthu_nhanvien_chitietvaora(false, date_ngayhethong.DateTime, frm_tungaydenngay.date_fromdatetime, frm_tungaydenngay.date_todatetime, com_khohang._GetselectedID(), radio_baocaoZ.Checked);
                    luoi_doanhthu.DataSource = dsbaocao.Tables[0];
                    txt_kieudoanhthu.Text = " Nhân viên chi tiết vào ra";
                    break;

                case "nv_chitiet":
                    str_loaibaocao = "nv_chitiet";
                    luoi_doanhthu.MainView = grid_nhanvien_chitiet;
                    luoi_doanhthu.DataSource = null;
                    dsbaocao = f_selectdoanhthu_nhanvien_chitiet(false, date_ngayhethong.DateTime, frm_tungaydenngay.date_fromdatetime, frm_tungaydenngay.date_todatetime, com_khohang._GetselectedID(), radio_baocaoZ.Checked);
                    luoi_doanhthu.DataSource = dsbaocao.Tables[0];
                    txt_kieudoanhthu.Text = " Nhân viên chi tiết";
                    break;

                case "nv_chitietmathang":
                    str_loaibaocao = "nv_chitietmathang";
                    luoi_doanhthu.MainView = grid_nhanvien_chitietmathang;
                    luoi_doanhthu.DataSource = null;
                   string SmAnv = this.sGetNhanVien_fromList();
                    dsbaocao = f_selectdoanhthu_nhanvien_chitietmathang(false, date_ngayhethong.DateTime, frm_tungaydenngay.date_fromdatetime, frm_tungaydenngay.date_todatetime, com_khohang._GetselectedID(), radio_baocaoZ.Checked, SmAnv);
                    luoi_doanhthu.DataSource = dsbaocao.Tables[0];
                    txt_kieudoanhthu.Text = " Nhân viên chi tiết mặt hàng";
                    break;

                case "kho":
                    str_loaibaocao = "kho";
                    luoi_doanhthu.MainView = grid_doanhthu_khohang;
                    luoi_doanhthu.DataSource = null;
                    dsbaocao = f_selectdoanhthu_khohang(false, date_ngayhethong.DateTime, frm_tungaydenngay.date_fromdatetime, frm_tungaydenngay.date_todatetime, com_khohang._GetselectedID(), radio_baocaoZ.Checked);
                    luoi_doanhthu.DataSource = dsbaocao.Tables[0];
                    txt_kieudoanhthu.Text = " Kho hàng";
                    break;

                case "kho_nhapxuat":
                    str_loaibaocao = "kho_nhapxuat";
                    luoi_doanhthu.MainView = grid_khonhapxuat_kho;
                    luoi_doanhthu.DataSource = null;

                    DateTime NGAYGIO1 = new DateTime(date_ngayhethong.DateTime.Year, date_ngayhethong.DateTime.Month, date_ngayhethong.DateTime.Day, sp.Hours, sp.Minutes, 0);
                    if (radio_baocaoZ.Checked)
                        NGAYGIO1 = new DateTime(date_ngay_baocao_z.DateTime.Year, date_ngay_baocao_z.DateTime.Month, date_ngay_baocao_z.DateTime.Day, sp.Hours, sp.Minutes, 0);

                    dsbaocao = f_selectdoanhthu_khohang_nhapxuat(false, NGAYGIO1, frm_tungaydenngay.date_fromdatetime, frm_tungaydenngay.date_todatetime, com_khohang._GetselectedID(), radio_baocaoZ.Checked);
                    luoi_doanhthu.DataSource = dsbaocao.Tables[0];
                    txt_kieudoanhthu.Text = " Kho hàng";
                    break;

                case "khachhang_tongquat":
                    str_loaibaocao = "khachhang_tongquat";
                    luoi_doanhthu.MainView = grid_khachhangtongquat_1;
                    luoi_doanhthu.DataSource = null;

                    DateTime NGAYGIO2 = new DateTime(date_ngayhethong.DateTime.Year, date_ngayhethong.DateTime.Month, date_ngayhethong.DateTime.Day, sp.Hours, sp.Minutes, 0);
                    if (radio_baocaoZ.Checked)
                        NGAYGIO2 = new DateTime(date_ngay_baocao_z.DateTime.Year, date_ngay_baocao_z.DateTime.Month, date_ngay_baocao_z.DateTime.Day, sp.Hours, sp.Minutes, 0);

                    dsbaocao = f_selectdoanhthu_khachhang_tongquat(false, NGAYGIO2, frm_tungaydenngay.date_fromdatetime, frm_tungaydenngay.date_todatetime, com_khohang._GetselectedID(), radio_baocaoZ.Checked);
                    luoi_doanhthu.DataSource = dsbaocao.Tables[0];
                    txt_kieudoanhthu.Text = "Khách hàng";
                    break;

                case "khachhang_mathang":
                    str_loaibaocao = "khachhang_mathang";
                    luoi_doanhthu.MainView = grid_khachhang_mathang;
                    luoi_doanhthu.DataSource = null;
                    dsbaocao = f_selectdoanhthu_khachhang_mathang(false, date_ngayhethong.DateTime, frm_tungaydenngay.date_fromdatetime, frm_tungaydenngay.date_todatetime, com_khohang._GetselectedID(), radio_baocaoZ.Checked);
                    luoi_doanhthu.DataSource = dsbaocao.Tables[0];
                    txt_kieudoanhthu.Text = "Khách hàng";
                    break;

                case "khachhang _ chi tietso":
                    str_loaibaocao = "khachhang_chitietso";
                    luoi_doanhthu.MainView = grid_khachhangchitietso;
                    luoi_doanhthu.DataSource = null;
                    dsbaocao = f_selectdoanhthu_khachhang_chitietso(false, date_ngayhethong.DateTime, frm_tungaydenngay.date_fromdatetime, frm_tungaydenngay.date_todatetime, com_khohang._GetselectedID(), radio_baocaoZ.Checked);
                    luoi_doanhthu.DataSource = dsbaocao.Tables[0];
                    txt_kieudoanhthu.Text = "Khách hàng";
                    break;

                case "Chi tiet phieu":
                    str_loaibaocao = "doanh thu phieu";
                    luoi_doanhthu.MainView = grid_nhanvien_phieu;
                    luoi_doanhthu.DataSource = null;
                    //if (radio_baocaoX.Checked == true)
                    //    dsbaocao = f_selectdoanhthu_nhanvien_phieu(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), Variable.manhanvien, false);
                    //else
                    //    dsbaocao = f_selectdoanhthu_nhanvien_phieu(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), Variable.manhanvien, true);
                    SmAnv = this.sGetNhanVien_fromList();
                    dsbaocao = f_selectdoanhthu_nhanvien_phieu(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID(), SmAnv, !radio_baocaoX.Checked);

                    luoi_doanhthu.DataSource = dsbaocao.Tables[0];
                    txt_kieudoanhthu.Text = "Khách hàng";
                    break;

                
            }

        }

        private void lbel_calendar_Click(object sender, EventArgs e)
        {
            date_ngay_baocao_z.ShowPopup();
        }

        private void label4_Click(object sender, EventArgs e)
        {
            date_ngayhethong.ShowPopup();
        }

        //private void lbel_doanhthu_theogio_Click(object sender, EventArgs e)
        //{
        //    mh_list_gio omh_list_gio = new mh_list_gio();
        //    TimeSpan sp = omh_list_gio.ShowForm();
        //    DateTime nNgay = new DateTime(date_ngayhethong.DateTime.Year, date_ngayhethong.DateTime.Month, date_ngayhethong.DateTime.Day, sp.Hours, sp.Minutes, 0);

        //}

        private void lbel_giamgia_Click(object sender, EventArgs e)
        {

        }

        private void lbel_vat_Click(object sender, EventArgs e)
        {

        }

        private void lbel_thuedichvu_Click(object sender, EventArgs e)
        {

        }

        private void lbel_tientip_Click(object sender, EventArgs e)
        {

        }

        private void lbel_tientrongket_Click(object sender, EventArgs e)
        {

        }

        private void lblBaocao_TuanThang_Click(object sender, EventArgs e)
        {
            if (!new cpublic().KiemTra_CoQuyen(this.Name, ((System.Windows.Forms.Control)sender).Name, 1))
            {
                MessageBox_LPV.Show("Bạn không có quyền");
                return;
            }
        }

        private void bton_xoabaocao_Click(object sender, EventArgs e)
        {
            int ketqua = -1;

            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Xóa báo cáo?", loaithongbao.yesno.GetHashCode());
            mh_tungaydenngay frm_tungaydenngay = new mh_tungaydenngay();
            frm_tungaydenngay.ShowDialog();


            if (Variable.thongbao == true)
            {
                ketqua = f_deletephieubanhang_theongay(frm_tungaydenngay.str_fromdate, frm_tungaydenngay.str_todate, com_khohang._GetselectedID());

                if (ketqua == -1)
                    mess.messagebox_pos("Thông báo", "Xóa không thành công", loaithongbao.thongtin.GetHashCode());
                else
                    mess.messagebox_pos("Thông báo", "Xóa thành công", loaithongbao.thongtin.GetHashCode());
            }

        }

        private void lbel_nguyenlieu_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;

            if (!new cpublic().KiemTra_CoQuyen(this.Name, ((System.Windows.Forms.Control)sender).Name, 1))
            {
                MessageBox_LPV.Show("Bạn không có quyền");
                return;
            }

            this.Cursor = Cursors.WaitCursor;
            try
            {
                LPV_POS.mh_pos.mh_inbaocao.frmBaoCao_NguyenVatLieu ofrmBaoCao_NguyenVatLieu = new LPV_POS.mh_pos.mh_inbaocao.frmBaoCao_NguyenVatLieu();
                ofrmBaoCao_NguyenVatLieu.ShowInTaskbar = false;
                ofrmBaoCao_NguyenVatLieu.ShowDialog();

            }
            catch
            {
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }

        }

        private void lbel_CongNo_KhachHang_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;
            if (!new cpublic().KiemTra_CoQuyen(this.Name, ((System.Windows.Forms.Control)sender).Name, 1))
            {
                MessageBox_LPV.Show("Bạn không có quyền");
                return;
            }
            LPV_POS.mh_pos.mh_inbaocao.frmBaoCao_CongNo_KhachHang ofrmBaoCao_CongNo_KhachHang = new LPV_POS.mh_pos.mh_inbaocao.frmBaoCao_CongNo_KhachHang();
            ofrmBaoCao_CongNo_KhachHang.ShowInTaskbar = false;
            ofrmBaoCao_CongNo_KhachHang.ShowDialog();
        }

        private void lbel_Bao_cao_LaiLo_Click(object sender, EventArgs e)
        {
            if (lbel_Bao_cao_LaiLo.Tag + "" != "")
            {
                LPV_POS.mh_pos.mh_inbaocao.frmBaoCao_LaiLo ofrmBaoCao_LaiLo = new LPV_POS.mh_pos.mh_inbaocao.frmBaoCao_LaiLo();
                ofrmBaoCao_LaiLo.ShowDialog();
            }
        }

        private void lbel_Ket_Thuc_Ngay_Click(object sender, EventArgs e)
        {
            if (lbel_Ket_Thuc_Ngay.Tag + "" != "")
            {
                frmbaocao_KetThuc_NgayLamViec ofrmbaocao_KetThuc_NgayLamViec = new frmbaocao_KetThuc_NgayLamViec(com_khohang._GetselectedID());
                ofrmbaocao_KetThuc_NgayLamViec.ShowDialog();
                if (ofrmbaocao_KetThuc_NgayLamViec.DialogResult == DialogResult.OK)
                {
                    this.mh_baocaodoanhthu_Load(null, null);
                    this.Dispose();
                }
            }
        }

        private void lblThe_Tra_Truoc_Click(object sender, EventArgs e)
        {
            if (new cpublic().Registry_Get("chkThanh_Toan_Bang_The_Tra_Truoc") == "True")
            {
                mh_baocaodoanhthu_TheTraTruoc frm = new mh_baocaodoanhthu_TheTraTruoc();
                frm.ShowDialog();
            }
        }

        private void lblHistory_Bill_Item_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;
            if (new cpublic().KiemTra_CoQuyen(this.Name, lblHistory_Bill_Item.Name, 1))
            {
                frmHistory frm = new frmHistory();
                frm.ClientSize = this.ClientSize;
                frm.ShowDialog();
            }
        }

        //ANHLQ
        private void LblBCNVPV_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;

            mh_baocao_nhanvienphucvu frm = new mh_baocao_nhanvienphucvu();
            frm.ShowDialog();
        }

        private void lblNguoiGioiThieu_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;

            mh_baocaodoanhthu_nguoigioithieu frm = new mh_baocaodoanhthu_nguoigioithieu();
            frm.ShowDialog();
        }

        private void lblBCThe_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;

            mh_baocaodoanhthu_nhomthe frm = new mh_baocaodoanhthu_nhomthe();
            frm.ShowDialog();
        }

        private void BtnGuiMail_Click(object sender, EventArgs e)
        {
            frmMailOption frm = new frmMailOption();
            frm.ShowDialog();
        }

        private void lbel_CongNo_NhaCungCap_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;
            frmBaoCao_CongNo_NhaCungCap frm = new frmBaoCao_CongNo_NhaCungCap();
            frm.ShowDialog();
        }

        //BAO CÁO DOANH THU THEO TUNG LOAI THE -- EDITOR: PL, DATE: 2012.07.27
        private void lbl_bcThe_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Control)sender).Tag + "" == "") return;
            if (!new cpublic().KiemTra_CoQuyen(this.Name, ((System.Windows.Forms.Control)sender).Name, 1))
            {
                MessageBox_LPV.Show("Bạn không có quyền");
                return;
            }
            ngaythang = new xulyngaythangnam();
            Variable.str_tungay = ngaythang.chuoingay(ngaythang.chuyenngay(date_ngayhethong.Text));
            luoi_doanhthu.MainView = grv_bcTheTT;
            luoi_doanhthu.DataSource = null;
            str_loaibaocao = "thethanhtoan";


            check_ngay.Checked = true;
            date_ngay_baocao_z.Enabled = true;
            date_ngayhethong.Enabled = true;
            com_khohang._SetselectedID("1");

            ds = f_selectdoanhthu_nhom(1, ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), !radio_baocaoX.Checked);
            dt_bcthe = f_bcThe(ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), !radio_baocaoX.Checked);

            if (dt_bcthe.Rows.Count < 0)
                return;
            else
            {
                luoi_doanhthu.DataSource = dt_bcthe;

                //--------bc theo hinh thuc thanh toan,-- editor: PL, date: 2012.07.25
                dtienmat = f_Tongtienmat(ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                decimal.TryParse(dtienmat.Rows[0]["TONGTIEN"] + "", out sumTienmat);

                dThe = f_TongThetratruoc(ngaythang.chuyenngay(date_ngayhethong.Text), ngaythang.chuyenngay(date_ngayhethong.Text), com_khohang._GetselectedID(), !radio_baocaoX.Checked);
                decimal.TryParse(dThe.Rows[0]["TONGTIEN"] + "", out sumThe);
                if (ds.Tables[0].Rows.Count > 0)
                    f_chitietdoanhthu(ds);

            }
        }

    }
}